onItemCheckedStateChanged()是在自定义ExpandableListView只调用一次 [英] onItemCheckedStateChanged() is called only once on custom ExpandableListView

查看:425
本文介绍了onItemCheckedStateChanged()是在自定义ExpandableListView只调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的自定义多选 ExpandableListView 这里是Java的code组布局:

I am trying to implement multiple selection in my custom ExpandableListView here is the Java code and group layout:

的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中,但我无法选择,甚至一个项目。我在想什么?下面是截图:

When I long click on a item the item get selected and ActionMode is created but if I want to select another item the item gets expanded and onItemCheckedStateChanged() is not called anymore so I can't select more than one item. I tried to put CHOICE_MODE_MULTIPLE_MODAL in XML but I couldn't select even one item. What am I missing? Here is a screenshot:

推荐答案

好吧,我终于有一个解决办法解决它。

Ok I finally solved it with a workaround.

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

所以,当我长preSS一些项目操作模式已启用,如预期中的第一项检查。要继续选择项目我设置了 OnGroupClickListener ,所以现在当我点击我选择一个项目/使用取消选中 setItemChecked()并返回真正时的操作模式使如此列表中没有得到扩展,只是选择的。

So when I long-press some item the Action Mode is enabled and the first item is checked as expected. To continue selecting items I set an OnGroupClickListener, so now when I click on an item I select/deselect it using setItemChecked() and return true when the Action Mode is enable so the list doesn't get expanded, just selected.

这篇关于onItemCheckedStateChanged()是在自定义ExpandableListView只调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆