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

Android OnItemCheckdStateChanged()在自定义ExpandableListView上仅调用一次,android,expandablelistview,Android,Expandablelistview,我正在尝试在我的自定义ExpandableListView中实现多个选择以下是Java代码和组布局: Java mExpandable.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE_MODAL); mExpandable.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override

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

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\u MODE\u MULTIPLE\u model
放入XML中,但我甚至无法选择一项。我错过了什么?以下是一个屏幕截图:


好的,我终于找到了解决办法

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;
    }
});

因此,当我长按某个项目时,动作模式将启用,第一个项目将按预期进行检查。为了继续选择项目,我在GroupClickListener上设置了一个
选项,所以现在当我单击一个项目时,我会使用
setItemChecked()
来选择/取消选择它,并在操作模式为enable(启用)时返回
true
,这样列表就不会展开,只是选择了而已。

您的答案对我来说几乎正确,但我有一个问题

问题就在这里:

@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)。但是这个方法的position=2!所以它选择了一个不存在的行。我认为这种行为是因为当孩子们被扩展时,第二组有索引2(在这个例子中)。如何解决正确使用CAB和组行的问题?就像是一个普通的ListView。

是的,我也有这个问题,我不知道如何修复它,所以我决定使用它来设置展开/折叠动作的动画。MultiChoiceMode不起作用,所以我将其移植到Android Studio,并将其修改为与MultiChoiceMode一起工作,请参见此处的代码:我尝试过,效果很好!我只是我有个问题。适配器启动listView.smoothScrollBy(..)后,它会在必要时使listView滚动,当我单击某个元素时,第一次单击将被忽略(您必须单击两次)。我试图用OnInterceptTouchEvent做一些事情,但在某些情况下总是失败。你也是这样吗?我不确定我是否理解你。当库中使用
listView.smoothScrollBy(..)
滚动列表以使单击的项目可见时,是否忽略下一次单击事件?我没有这个问题。@DIFTOR给我发电子邮件,给我一个屏幕截图或视频,显示正在发生的事情,也许我可以帮助你。Andres,首先,非常感谢你提供的源代码。我已经尝试在Eclipse中导入您在这个链接上发布的带有多选码的源代码,但它不起作用。我知道你是在安卓工作室做的。如何使它在Eclipse中工作?谢谢你,我认为你应该从源代码创建一个项目,或者你可以创建一个项目并复制。java文件