我想要完成的是在每一行中都有一个复选框,能够单独选中该框(用于批量删除)并能够选择整行以查看与列表项关联的数据。
我用 textview 做了一个复选框,但只能选中该框,我无法单击列表项来查看该项目数据。我还使用了 checkedTextView
,但是在您单击调用 onListItemClick
的行时会选中该框,这也不是我想要的。是否有一些我可以将选中复选框与单击 ListView 项目分开的东西?
在选择要删除和查看的邮件时,几乎是在尝试做 gmail
应用程序所做的事情
这是我的行布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/nameCheckTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:paddingTop="5dp"
android:paddingLeft="15dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
创建 ListView
@Override
public void onActivityCreated(Bundle state){
super.onActivityCreated(state);
lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(false);
setEmptyText("No Bowlers");
registerForContextMenu(getListView());
populateList();
}
编辑:
我的填充方法
public void populateList(){
String[] fields = new String[] {BowlersDB.NAME};
//mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,null,fields,new int[] {R.id.nameCheckTV});
mAdapter = new SimpleCursorAdapter(getActivity(),R.layout.check_listview,null,fields,
new int[] {R.id.nameCheckTV});
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
最佳答案
问题是 Android 不允许您选择具有可聚焦元素的列表项。尝试修改列表项上的复选框:
android:focusable="false"
关于java - 在 ListView 中使用复选框和 onListItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897994/