3

我正在使用用于我的ListView的自定义适配器。创建 ArrayList 后

WifiListAdapter<WifiListItem> adapter = new WifiListAdapter<WifiListItem>(
            this, R.layout.listview_item_text, listItems);

我的应用程序中有以下代码:

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);

但是,当我尝试单击复选框时,没有任何反应。所以我必须手动管理切换复选框状态。

    protected void onListItemClick(ListView l, View v, int position, long id) {
    CheckedTextView checkBox = (CheckedTextView) v
            .findViewById(R.id.text1);
    if (checkBox != null) {
        checkBox.toggle();

然后它工作。(在此之前我必须删除 setChoiceMode 方法调用)

那么可能是什么问题呢?为什么没有效果?

我的 R.layout.listview_item_text.xml

    <TextView
    android:id="@+id/topText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:paddingTop="7dp"
    android:textColor="?android:attr/textColorPrimary"/>

<TextView
    android:id="@+id/botText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="17sp"
    android:paddingTop="2dp"
    android:paddingBottom="7dp"
    android:textColor="?android:attr/textColorTertiary"
 />

在我的适配器中,我使用以下代码扩展复选框布局:

                convertView = mInflater.inflate(
                        R.layout.listview_item_checkbox, null);

            CheckedTextView checkbox = ((CheckedTextView) convertView
                    .findViewById(R.id.text1))

listview_item_checkbox.xml

    <CheckedTextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textSize="22sp"
    android:paddingTop="7dp"
    android:paddingBottom="7dp"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
     />
4

3 回答 3

3

而不是您的代码:

if (checkBox != null)
        checkBox.toggle();

尝试这样的事情:

if (checkBox != null) checkBox.setChecked(l.getCheckedItemPositions().get(position));

如果可以 - 改进您的代码(创建CheckableFrameLayout为列表项的父布局)。ListView保留已检查位置的BooleanSparseArray(您可以使用getCheckedItemPositions ()方法获取它)。在代码 ( grepcode ) 中,它View仅在View实现Checkable时自行设置是否已检查。如果 createCheckableFrameLayout和 sat 作为主父级,则不必在适配器中处理这些情况。

于 2012-12-20T11:20:28.563 回答
2

我相信您必须使用实现Checkable的小部件。尝试使用android.R.layout.simple_list_item_multiple_choice. 如果你需要两行,那么实现类似的东西。

于 2012-12-20T09:27:59.320 回答
0

The thing is that there is some problem in the source code (event for lastest version at the time I am writing this answer, 4.3r2.1). Here is the problem:

1899        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
1900            if (child instanceof Checkable) {
1901                ((Checkable) child).setChecked(mCheckStates.get(position));
1902            } else if (getContext().getApplicationInfo().targetSdkVersion
1903                    >= android.os.Build.VERSION_CODES.HONEYCOMB) {
1904                child.setActivated(mCheckStates.get(position));
1905            }
1906        }

Source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/widget/ListView.java?av=f

The easiest answer to your question, if your minSDK is 11, is customize the checkbox like this:

<?xml version="1.0" encoding="utf-8"?>
<selector
 xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>
 <item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />
 <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />
 <item android:state_checked="true" android:drawable="@drawable/checkbox_selected" />
</selector>

Save it to the drawable folder as xml file and refer it as layout.

The middle level solution is set onItemClickListener and perform different behavior depending on the choice mode.

Here is the source code which works: https://github.com/jiahaoliuliu/CustomizedListRow

And the hard but correct solution is the one from MarvinLab, which you can find it here: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

Related issue: What is the difference between the states selected, checked and activated in Android?

于 2013-09-04T14:00:53.277 回答