Java 如何在ListView中启用批处理上下文操作?
我正试图密切关注Android官方指南,但我一直无法在我的ListView项目长按中激活上下文操作视图Java 如何在ListView中启用批处理上下文操作?,java,android,Java,Android,我正试图密切关注Android官方指南,但我一直无法在我的ListView项目长按中激活上下文操作视图 public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadList(); // loads Cursor data and sets list adapter in AsyncTask
ListView listView = getListView();
listView.setLongClickable(true); // no effect
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
// implemention of MultiChoiceModeListener
});
}
// rest of the class
}
以下是我的列表\u row\u item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?android:attr/activatedBackgroundIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/reminder_row_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/reminder_row_interval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/reminder_row_description"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Switch
android:id="@+id/switch_reminder_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/reminder_row_interval"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/reminder_row_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/reminder_row_interval"
android:layout_below="@+id/reminder_row_interval"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
和我的活动_main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
我曾尝试使用listView.setOnItemLongClickListener()
手动启动ActionMode,但虽然启动了上下文操作模式,但我无法进行多次选择,而且没有教程/指南需要这样做。我错过了一些东西
基本上,我想实现果冻豆闹钟中所看到的效果:
任何指点都将不胜感激 基于@Luksprog comment,从onCreateActionMode()返回true将解决您的问题您的
MultichoiceModelListener
看起来怎么样?目前还不多onCreateActionMode
对关联菜单进行充气onActionItemClicked
设置为不执行任何操作onItemCheckedStateChanged
基本上有一个设置标题的健全性检查。但这些都没有发生。调试器证明它根本没有被调用。我正在为列表适配器使用SimpleCrsorAdapter。这可能是问题所在吗?也许我需要手动执行我自己的操作模式。我希望您从onCreateActionMode()
返回true
。感谢@Luksprog指出,这解决了我的问题!干杯!:)