android - 在 ListView 中检查多个项目

标签 android android-layout

我使用适配器从内容提供程序加载 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"

如何让用户在运行时更改每个项目的检查状态?

最佳答案

引用这个链接...

CheckedTextView

这可能对你有帮助..

[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/

相关文章:

Android:Row.xml 的相对布局

android - Invalidate() 实际上并没有重绘 child

android - BottomSheetBehavior 与两个 RecyclerView

android - 如何从另一个 fragment 调用 Activity 。?

php - 如何从 PHP 网站向 iOS 和 Android 发送推送通知?

机器人 : layout_weight not picking values from correct layout on orientation change

android - 如何以编程方式使小按钮包裹小文本

安卓载入圈

android: getFragmentManager() 返回 null

android - 如何使更新进度中的ProgressBar "smoother"