【Android】ListView多选模式的使用
16lz
2021-01-26
【关键词】
ListView
多选
【问题】
- 如何使用ListView的多选模式?
【效果图】
【分析】
- 长按某一个 item 的时候,开启多选模式,并让当前选择的 item 处于选中状态;
- 利用ListView 的 Choice属性,可以很方便使用多选模式或单项模式;
【解决方案】
使用方法
长按 item 开启多选模式
final ListView lvMain = (ListView) view.findViewById(R.id.lv_main);
// 长按开启多选模式;
lvMain.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView instanceof AbsListView) {
// 当前处于None时才震动(避免多次调用),并开启多选模式;
if (((AbsListView) adapterView).getChoiceMode() == AbsListView.CHOICE_MODE_NONE) {
//注意: 震动需要权限;
Vibrator vibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(100);
enableMultipleChoice(true);
// 设置当前长按项被选中;
mLouAdapter.getBindListView().setItemChecked(i, true);
return true;
}
}
return false;
}
});
private void enableMultipleChoice(boolean enable) {
if (enable) {
mLouAdapter.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} else {
mLouAdapter.clearChoice();
}
}
关闭多选模式
enableMultipleChoice(false);
【代码】
-
结合上一节的【Android】通用Adapter,在[LouAdapter.java]中添加以下代码
// -------- 添加 choice调用(2016.03.25)
public void clearChoice() {
mListView.clearChoices();
updateChange(); // 此句是必须的,否则界面无法更新;
mListView.post(new Runnable() {
@Override
public void run() {
// 注意需要使用Runnable才能生效;
// 参考资料[ListView selection remains persistent after exiting choice mode] (http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode)
mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
});
}
public void setChoiceMode(int mode) {
mListView.setChoiceMode(mode);
}
public int getChoiceMode() {
return mListView.getChoiceMode();
}
public void deleteChoicedItem() {
if (mListView.getChoiceMode() != AbsListView.CHOICE_MODE_NONE) {
// 获取被选中的ITEM;
SparseBooleanArray sparseBooleanArray = mListView.getCheckedItemPositions();
ArrayList<T> deleteLists = new ArrayList<T>();
for (int i = 0; i < sparseBooleanArray.size(); i++) {
if(sparseBooleanArray.valueAt(i)){
deleteLists.add(mLists.get(sparseBooleanArray.keyAt(i)));
}
}
mLists.removeAll(deleteLists);
mListView.clearChoices();
updateChange();
}
}
// ~~~~~~~~~~~~
-
使用过程中需要注意,需要在 ListView的item_layout的根布局中添加背景(否则,不能看到选中状态)
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="88dp"
android:paddingBottom="4dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:background="@drawable/lv_item_selector"
android:paddingTop="8dp">
[lv_item_selector.xml]
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@color/lv_item_state_activated_pressed" android:state_activated="true" android:state_pressed="true"/>
- android:drawable="@color/lv_item_state_activated" android:state_activated="true"/>
- android:drawable="@color/lv_item_state_pressed" android:state_pressed="true"/>
- android:drawable="@color/lv_item_state_normal"/>
[colors.xml]
name="lv_item_state_activated">#994455 name="lv_item_state_activated_pressed">#d72e50 name="lv_item_state_pressed">#88eec99d name="lv_item_state_normal">#e29944
【参考资料】
更多相关文章
- Android(安卓)launchMode
- Android(安卓)Camera HAL3中预览preview模式下的控制流
- Android(安卓)欢迎界面及首次安装导航界面实例
- Android(安卓)实现类似美团的菜单效果
- [Android]studio报异常:...com.android.ide....java.lang.Unsuppo
- Android的framework层是如何对铃声的音量进行调整的
- Android中launchMode模式详解
- Android四种模式里隐含的哲学
- 安卓 activity的四种启动模式