简体   繁体   English

如何使listView项目和其中的视图同时出现?

[英]How to make listView item and view out of it appear at the same time?

my wish is, to have a listView and when i choose delete in the contextmenu appearing when I do a longclick on a listview item, there should appear checkboxes in each listview element and a buttonbar on the top and one on the bottom...and all at the same time. 我的愿望是要有一个listView,当我在对listview项进行长按时选择出现在上下文菜单中的delete时,每个listview元素中应出现复选框,顶部应有一个按钮栏,底部应有一个按钮栏...以及都在同一时间。 So i wrote the code following below. 所以我写了下面的代码。 But when I click on delete, only the buttonbars appear and I have to do a second click on delete to make the checkboxes appear. 但是,当我单击删除时,只有按钮栏出现,并且我必须再次单击删除以使复选框出现。 By testing with different "configurations" of my code (adding or removing some parts) and checking everything step by step with the debugger I found out, that the checkboxes appear immediately, when I remove the buttonbars and all code is reached. 通过对代码的不同“配置”进行测试(添加或删除某些部分)并使用调试器逐步检查所有内容,我发现,当我删除按钮栏并到达所有代码时,复选框立即显示。 So now my question is: Does anyone know, why the checkboxes and the bars don't appear on the same click and what to make, so they do? 所以现在我的问题是:谁知道,为什么复选框和条形图不会出现在同一单击上,而怎么做,却如此?

public boolean onContextItemSelected(MenuItem menu) 
{ 
AdapterView.AdapterContextMenuInfo info = (AdapterView
    .AdapterContextMenuInfo) menu.getMenuInfo();
ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
tripTracker.findViewById(R.id.buttonBar).setVisibility(View.VISIBLE);
tripTracker.findViewById(R.id.buttonBarCB).setVisibility(View.VISIBLE); 
checkBox(false, View.VISIBLE);
((CheckBox) tripList.findViewById(info.targetView.getId())
    .findViewById(R.id.checkBox)).setChecked(true); 
return true; 
}

private void checkBox(boolean checked, int visibility)
{
    ListView tripList = (ListView) tripTracker.findViewById(R.id.tripList);
    for (int i = 0; i < tripList.getAdapter().getCount(); i++)
    {
        int id = ((Trip) tripList.getAdapter().getItem(i)).getId();
        CheckBox cb = (CheckBox) tripList.findViewById(id).findViewById(
            R.id.checkBox);
        cb.setVisibility(visibility);
        cb.setChecked(checked);
    }
}

XML with ListView 具有ListView的XML

    <LinearLayout
      android:id="@+id/buttonBarCB"
      style="?android:attr/buttonBarStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="gone" >

        <Button
          android:id="@+id/allCB"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/all" />

        <Button
          android:id="@+id/noneCB"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/none" />
    </LinearLayout>

    <TextView
      android:id="@+id/noTrip"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="@dimen/activity_horizontal_margin"
      android:text="@string/noTrip" />

    <ListView
      android:id="@+id/tripList"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1" >

    </ListView>

    <LinearLayout
      android:id="@+id/buttonBar"
      style="?android:attr/buttonBarStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="gone" >

        <Button
          android:id="@+id/cancelTr"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:text="@android:string/cancel" />

        <Button
          android:id="@+id/okTr"
          style="?android:attr/buttonBarButtonStyle"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.5"
          android:text="@android:string/ok" />
    </LinearLayout>

</LinearLayout>

XML for rows 行的XML

    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent" >

        <CheckBox
          android:id="@+id/checkBox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerVertical="true"
          android:visibility="gone" />

    </RelativeLayout>

    <RelativeLayout
      android:id="@+id/RelativeLayout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

        <TextView
          android:id="@+id/h1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:text="Large Text"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="?android:attr/textColorPrimaryInverse" />

        <TextView
          android:id="@+id/date"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignTop="@+id/h1"
          android:text="date"
          android:textColor="?android:attr/textColorSecondaryInverse" />

        <TextView
          android:id="@+id/h2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/h1"
          android:layout_below="@+id/h1"
          android:text="Small Text"
          android:textAppearance="?android:attr/textAppearanceSmall"
          android:textColor="?android:attr/textColorSecondaryInverse" />
    </RelativeLayout>

</LinearLayout>

I realy suggest you do not reinvent the wheel and use Contexual Action Bar . 我真的建议您不要重新发明轮子,而要使用Contexual Action Bar Here you can find my example how to select rows in listview and work with them. 在这里,您可以找到我的示例如何在列表视图中选择行并对其进行处理。

And of cause, do not forget to call adapter.notifyDataSetChanged() , after changes of data that is under adapter. 因此,不要忘记在适配器下的数据更改后调用adapter.notifyDataSetChanged()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM