onItemCheckedStateChanged()仅在自定义ExpandableListView上调用一次

时间:2014-01-24 23:53:34

标签: android expandablelistview

我正在尝试在我的自定义ExpandableListView中实现多个选择,这里是Java代码和组布局:

爪哇

mExpandable.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE_MODAL);
        mExpandable.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
                if(DEBUG) Log.i("mFragmentList","onItemCheckedStateChanged()");

                if(mExpandable.getCheckedItemCount() == 0)
                    actionMode.setSubtitle("Check some items!");
                else if(mExpandable.getCheckedItemCount() == 1)
                    actionMode.setSubtitle("1 item selected");
                else
                    actionMode.setSubtitle(mExpandable.getCheckedItemCount() + " items selected");
            }

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, android.view.Menu menu) {
                actionMode.setTitle("Select Items");
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, android.view.Menu menu) {
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });

group_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/activated">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:scaleType="center"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:layout_gravity="left"
        android:src="@drawable/ball" />

    <TextView
        android:id="@+id/tvGroupName"
        android:paddingLeft="35dp"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFF0000"
        android:textSize="17sp" />

</RelativeLayout>

当我长时间点击某个项目时,该项目会被选中并创建ActionMode但是如果我想选择另一个项目,该项目会被扩展并且onItemCheckedStateChanged()不再被调用,所以我无法选择不止一个项目。我试图将CHOICE_MODE_MULTIPLE_MODAL放在XML中,但我甚至无法选择一个项目。我错过了什么?这是一个截图:

enter image description here

2 个答案:

答案 0 :(得分:4)

好的,我终于通过解决方法解决了这个问题。

mExpandable.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                if(actionModeEnabled)
                    expandableListView.setItemChecked(i, !expandableListView.isItemChecked(i));

                return actionModeEnabled;
            }
        });

mExpandable.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
        if(mExpandable.getCheckedItemCount() == 1)
            actionMode.setSubtitle("1 item selected");
        else
            actionMode.setSubtitle(mExpandable.getCheckedItemCount() + " items selected");
    }

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, android.view.Menu menu) {
        actionModeEnabled = true;
        actionMode.setTitle("Select Items");
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, android.view.Menu menu) {
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        actionModeEnabled = false;
    }
});

因此,当我长按某个项目时,将启用“操作模式”,并按预期检查第一个项目。要继续选择项目,我设置了OnGroupClickListener,所以现在当我点击某个项目时,我使用setItemChecked()选择/取消选择它,并在启用操作模式时返回true,这样列表就不会得到扩展,刚刚被选中。

答案 1 :(得分:0)

你的回答对我来说几乎是正确的,但我有一个问题。

问题在于:

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
    if(mExpandable.getCheckedItemCount() == 1)
        actionMode.setSubtitle("1 item selected");
    else
        actionMode.setSubtitle(mExpandable.getCheckedItemCount() + " items selected");
}

当群组崩溃时,它可以正常工作。但是,例如,如果我有这个结构:

- Group 1
    + Child 1
- Group 2
    + Child 2

如果我在组2中长按,则我希望onItemCheckedStateChanged中的位置为1(因为我们从0开始,组1 =位置0,组2 =位置1)。但该方法的位置= 2!所以它选择一个不存在的行。我认为这种行为是因为当孩子们被扩展时,第2组有索引2(在示例中)。如何解决这个问题,以便正确使用CAB和组行?好像这是一个普通的ListView。