onItemCheckedStateChanged调用了两次

时间:2016-10-26 23:03:18

标签: android listview android-actionmode

我有一个奇怪的问题,我不确定究竟是什么。我设法通过手动检查列表视图中第一个被检查的项目是否正在接收第二个呼叫来绕过这个问题。

问题是当用户长按listview项(适配器内部的onlongclicklistener)时,会检查listview项。项目发布后(长按发布),项目设置为isChecked = false。我无法弄清楚导致这个问题的原因。如果我理解correclty,setMultiChoiceModeListener有自己的onlongclick侦听器被调用。有没有办法覆盖它并像大多数应用程序一样实现相同的功能(长按启动选择程序)?

适配器点击监听器

vi.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.d("test abc", "ovo je krenulo");
                ((MainActivity) mContext).list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
                ((MainActivity) mContext).lvPos = position;
                ((MainActivity) mContext).lvPosChecked = true;

                if(!((MainActivity) mContext).ListSelectionInProgress)
                {
                    ((MainActivity) mContext).ListSelectionInProgress = true;
                }

                if(!((MainActivity) mContext).list.isItemChecked(position))
                {
                    ((MainActivity) mContext).list.setItemChecked(position, true);
                }
                else
                {
                    ((MainActivity) mContext).list.setItemChecked(position, false);
                }
                return true;
            }
        });

        vi.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("CL", "State called");

                if(((MainActivity) mContext).ListSelectionInProgress)
                {
                    ((MainActivity) mContext).lvPos = position;
                    ((MainActivity) mContext).lvPos = -1;
                    if(!((MainActivity) mContext).list.isItemChecked(position))
                    {
                        ((MainActivity) mContext).list.setItemChecked(position, true);
                    }
                    else
                    {
                       ((MainActivity) mContext).list.setItemChecked(position, false);
                    }

                    Log.d("State", Integer.toString(((MainActivity) mContext).list.getCheckedItemCount()));
                }
            }

        });

setMultiChoiceModeListener

list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                if(position == lvPos && checked != lvPosChecked)
                {
                    list.setItemChecked(position, true);
                }
                mode.setTitle(Integer.toString(list.getCheckedItemCount()));
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu)
            {
                ListSelectionInProgress = true;
                MenuInflater inflater = mode.getMenuInflater();
                if(SUBLEVEL == 1) {
                    inflater.inflate(R.menu.action_mode_add, menu);
                }
                else if(SUBLEVEL == 5) // SATNICA
                {
                    inflater.inflate(R.menu.action_mode_remove, menu);
                }

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

                int c = 0;
                switch(item.getItemId())
                {
                    case R.id.selAll:
                        for(int i=0;i<list.getAdapter().getCount();i++) {
                            list.setItemChecked(i, true);
                        }
                        break;
                    case R.id.addSch:
                        c = 0;
                        for(int i=0;i<list.getAdapter().getCount();i++)
                        {
                            if(list.isItemChecked(i))
                            {
                                addToSchedule(i);
                                c++;
                            }
                        }
                        if(c > 1)
                        {
                            Toast.makeText(MainActivity.this, R.string.addToMyScheduleSuccessPlural, Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this, R.string.addToMyScheduleSuccess, Toast.LENGTH_SHORT).show();
                        }
                        mode.finish();
                        break;
                    case R.id.remSch:
                        c = 0;
                        if(list.getAdapter() != null)
                        {
                            for(int i=0;i<list.getAdapter().getCount();i++)
                            {
                                if(list.isItemChecked(i))
                                {
                                    removeFromSchedule(i);
                                    c++;
                                }
                            }
                            if(c > 1) {
                                Toast.makeText(MainActivity.this, R.string.removeFromMyScheduleSuccessPlural, Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                Toast.makeText(MainActivity.this, R.string.removeFromMyScheduleSuccess, Toast.LENGTH_SHORT).show();
                            }
                            loadMySchedule();
                            mode.finish();
                        }

                        break;
                }

                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                ListSelectionInProgress = false;
            }
        });

listview_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/classItemLV"
    android:background="@drawable/list_row"
    android:minHeight="80dp"
    android:descendantFocusability="blocksDescendants"
    android:elevation="2dp">

    <RelativeLayout
        android:layout_width="16dp"
        android:layout_height="wrap_content"
        android:id="@+id/lday"
        android:layout_alignParentStart="true"
        android:layout_marginStart="10dp"
        android:layout_centerVertical="true"
        android:background="@color/colorPrimary"
        android:minWidth="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="P\nO\nN"
            android:id="@+id/DAY1"
            android:layout_alignParentStart="false"
            android:textColor="#fff"
            android:textSize="15sp"
            android:layout_margin="2dp"
            android:textAlignment="center"
            android:layout_centerInParent="true" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lTime"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@+id/lday">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="10"
            android:id="@+id/timeFrom"
            android:textColor="@color/colorLV3Dark"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="12"
            android:id="@+id/timeTo"
            android:textColor="@color/colorLV3Dark"
            android:layout_below="@+id/timeFrom" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lInfo"
        android:layout_marginStart="10dp"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/lTime">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Analiza financijskih izvještaja"
            android:id="@+id/className"
            android:textColor="@color/colorLV3Dark"
            android:layout_alignParentStart="true"
            android:layout_marginEnd="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="prof. dr. sc. Ivan Ivić"
            android:id="@+id/classTutor"
            android:focusableInTouchMode="false"
            android:textColor="@color/colorLV3"
            android:layout_below="@+id/className" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="PREDAVANJE - DV41"
            android:id="@+id/classTypePlace"
            android:textIsSelectable="false"
            android:textColor="@color/colorLV3"
            android:textSize="12sp"
            android:layout_below="@+id/classTutor" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Datum: 5.10.-25.1."
            android:id="@+id/classDate"
            android:clickable="false"
            android:textSize="12sp"
            android:textColor="@color/colorLV3"
            android:layout_alignParentEnd="true"
            android:layout_alignBottom="@+id/classTypePlace"
            android:layout_marginEnd="10dp" />
    </RelativeLayout>

</RelativeLayout>

我尽可能地调试了代码,并注意到以下内容:

1)在长按发布后调用onItemCheckedStateChanged

2)OnClick被正常调用,根本不受longclick监听器的影响(除了通过公共布尔变量启用选择)

3)如果我将手指移到视图之外或其另一侧,在按住触摸事件的同时,它会保持检查状态

4)如果我快速选择一个不同的项目,两个都保持检查

似乎某些内容覆盖了我的listview项目的已检查状态。它可能是什么?

1 个答案:

答案 0 :(得分:1)

经过两天的挠头,我终于找到了解决方案。我正在处理适配器内部的正常和长时间点击。

似乎

setMultiChoiceModeListener

自己处理这两种情况,无需手动处理这两种类型的点击。我希望我早点读到它。我希望这个答案也可以帮助将来的某个人。