我使用适配器从内容提供程序加载 ListView 。列表中的每个项目都使用以下布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id ="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="@+id/txt_chat"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
我已将列表项设置为允许多选模式:
onCreate(){
...
lv_chatMessages = (ListView) findViewById(R.id.listview_chat);
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv_chatMessages.setItemsCanFocus(false);
...
}
但是在运行时,我无法检查 ListView 中的任何项目。如果我触摸该框,该项目只会暂时突出显示,但不会出现复选标记。
如果我向 CheckedTextView
添加属性,我可以为所有项目设置默认值:
android:checked="true"
如何让用户在运行时更改每个项目的检查状态?
最佳答案
引用这个链接...
这可能对你有帮助..
[Faizal 编辑]: 在 onItemClickListener 中使用 toggle() 可以达到目的:
lv_chatMessages.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
CheckedTextView ctv = (CheckedTextView) (((ViewGroup) arg1).findViewById(R.id.txt_chat));
ctv.toggle();
}
});
关于android - 在 ListView 中检查多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992634/