-1

I am trying to implement in an Android application a multiple items selection in an Android ListView to allow the user to delete severals rows in one action with the help of the action mode.

For example, I check the first item listening to the long click (in the following example, contacts is a ListView) :

@override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
  contacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
  contacts.setItemChecked(position, true);

  return true;
}

The matter is that after calling the setItemChecked() method, getCheckedItemPositions() method return null but it should return the position of the item that has been checked into the onItemLongClick() method no ?

The items of my ListView are made with a custom view. So I read all over the internet that my custom view have to implement the Checkable interface. So here the main container of my custom view :

public final class CheckableLinearLayout
    extends LinearLayout
    implements Checkable
{

  private boolean checked;

  public CheckableLinearLayout(Context context, AttributeSet attrs)
  {
    super(context, attrs);
  }

  @Override
  public boolean isChecked()
  {
    return checked;
  }

  @Override
  public void setChecked(boolean checked)
  {
    this.checked = checked;
  }

  @Override
  public void toggle()
  {
    checked = !checked;
  }

}

and here the layout of an item :

<?xml version="1.0" encoding="utf-8"?>
<com.package.CheckableLinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="10dip"
  android:background="@drawable/bg_contact"
>

  <ImageView
    android:id="@+id/contactPhoto"
    android:layout_width="75dip"
    android:layout_height="75dip"
  />

  <TextView
    android:id="@+id/contactName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dip"
    android:layout_gravity="center_vertical"
    android:textColor="@color/black"
  />
</com.package.CheckableLinearLayout>

I hope that someone will be able to help me !

Thank you in advance !

4

2 回答 2

0

你不需要打电话setItemChecked()手动调用。

只需设置 ListView (以前)

contacts.setChoiceMode(CHOICE_MODE_MULTIPLE_MODAL);
contacts.setMultiChoiceModeListener(new MultiChoiceModeListener() { ... });

当你长按一个项目时,它会被自动检查(并且onCreateActionMode()会被选择模式监听器调用)。

于 2014-06-06T22:23:14.203 回答
0

我终于找到了解决方案。

原来的onItemLongClick方法是这样的:

@Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
  {
    if (actionMode != null)
    {
      return false;
    }

    contacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    contacts.setItemChecked(position, true);
    getActionBarActivity().startSupportActionMode(new ActionModeCallback());

    return true;
  }

我将该getCheckedItemPositions()方法称为我的动作模式类的方法。

如果我在设置选择模式之前重写以前的方法getActionBarActivity().startSupportActionMode(new ActionModeCallback());,它似乎可以工作:)

@Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
  {
    if (actionMode != null)
    {
      return false;
    }

    getActionBarActivity().startSupportActionMode(new ActionModeCallback());
    contacts.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    contacts.setItemChecked(position, true);

    return true;
  }

:)

于 2014-06-07T10:13:03.840 回答