【发布时间】:2014-06-07 18:13:03
【问题描述】:
我正在尝试在 Android 应用程序中实现一个 Android ListView 中的多项选择,以允许用户在操作模式的帮助下在一个操作中删除多行。
例如,我检查第一个监听长按的项目(在下面的例子中,contacts 是一个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;
}
问题是,调用setItemChecked()方法后,getCheckedItemPositions()方法返回null,但是应该返回已经签入onItemLongClick()方法的item的位置没有?
我的ListView 的项目是用自定义视图制作的。所以我在网上读到我的自定义视图必须实现Checkable 接口。所以这里是我的自定义视图的主要容器:
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;
}
}
这里是项目的布局:
<?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>
希望有人能帮助我!
提前谢谢你!
【问题讨论】:
标签: android android-listview multipleselection android-actionmode