android,带复选框的列表视图

android, listview with checked box

我已经阅读了一些关于此事的帖子,但仍然无法正常工作。我想要一个带有复选框的列表视图。这是我的 activity_choose_songs.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/songs_to_choose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sd_cards_playlist"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="30sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:layout_below="@+id/songs_to_choose">
    </ListView>

</RelativeLayout>

这是特定项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp"
    android:background="@drawable/list_selector">

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:padding="10dp"
        android:gravity="center"
        android:drawableStart="?android:attr/listChoiceIndicatorMultiple"
        android:textColor="#f3f3f3" />

</LinearLayout>

这是 activity 中 onCreate() 方法的摘录,我希望在其中显示此列表视图:

ListAdapter adapter = new SimpleAdapter(getApplicationContext(),chosenSongsListData,
                R.layout.playlist_checked_item,new String[]{"songTitle"},new int[]{R.id.checkedTextView});

        setListAdapter(adapter);
        ListView lw = getListView();

        lw.setItemsCanFocus(false);
        lw.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

        lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

然而,当我启动 activity 并尝试检查它时,它看起来像(屏幕处于按下状态):

我的方法有什么问题?

您必须在您的项目点击侦听器中执行此操作,下面给出了示例代码。

       lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckBox check = (CheckBox) view.findViewById(R.id.checkedTextView);

            check.toggle(); //check or un-check the checkbox

            }
        });

我认为问题在于您的项目视图中有一个 LinearLayout。它无法将其状态传播到 children。

我想你有两个选择。

第一个是删除 LinearLayout。您的视图目前未实施 Checkable。当你删除它时 CheckedTextView 应该可以工作。

第二个选项是使用 CheckedLinearLayout 并让您的 children 具有此属性:

  android:duplicateParentState="true"`

您可以在线找到 CheckedLinearLayout 的实现。