Android ActionBar/ActionBarSherlock 多项选择微调器

标签 android listview checkbox actionbarsherlock spinner

我有一个 ListView,其中包含属于一个或多个类别的项目。我想通过单击操作栏中的图标来选择和取消选择这些类别。这样,listView 就会根据选择的类别进行刷新。

这是我找到的一个例子:

581753Screenshot20140110103007.png http://www.hostingpics.net/viewer.php?id=581753Screenshot20140110103007.png

目前,我找到了 2 个解决方案:

  • 添加一个带有可检查项目的微调器,但它会在每次关闭菜单 选择/取消选择
  • 在 RelativeLayout 中创建一个带有 cheackable 项目的 ListView 并制作 单击该图标时会出现。

第二个解决方案完全符合 UI 预期,但我认为有一种多选微调器解决方案。

最佳答案

Spinner 使用 ListPopupWindow 显示下拉菜单,您可以使用它来显示多选项目选择列表:

private void showPopup() {
    final ListPopupWindow lpw = new ListPopupWindow(this);
    lpw.setAdapter(/*Your adapter here*/);
    lpw.setAnchorView(mAnchor); // see below
    lpw.setContentWidth(/*specific value*/); // see below
    lpw.show();
    // this is required because the popup's `ListView` will not be available 
    // until the ListPopupWindow is actually shown.
    mAnchor.post(new Runnable() {
        @Override
        public void run() {
            lpw.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        }
    });
}

You could then call this method from the onOptionsItemSelected() callback when the right MenuItem is selected.还有两件事你需要注意:

mAchor 是一个View,您需要将其插入右上角的Activity 布局中,因此ListPopupWindow 将显示在正确的位置。例如,如果您有一个 Activity 根:

RelativeLayout 然后 mAchor 将是:

mAnchor = new View(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(0, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mAnchor.setLayoutParams(rlp);
// add mAnchorView first to the RelativeLayout

LinearLayout 然后 mAchor 将是:

mAnchor = new View(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(0, 0);
llp.gravity = Gravity.RIGHT;
mAnchor.setLayoutParams(llp);
// add mAnchorView first to the LinearLayout(assuming orientation vertical)

等等其他类型的布局。

其次,您需要将 ListPopupWindow 的宽度设置为所需的值。您需要针对不同的屏幕尺寸和方向(例如手机纵向和手机横向、纵向和横向的不同表格尺寸)调整此值。

关于Android ActionBar/ActionBarSherlock 多项选择微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040803/

相关文章:

onchange 赋值的 JavaScript 位置

ASP.NET 使用复选框隐藏 Gridview 行

android - 使用 cancel() 从 AlarmManager 删除警报 - Android

Android:如何清除自定义 ListView 中的所有项目

javascript - 如何使用 React Native ListView 高效搜索数据

Android:在 MyAdapter::getView 中计算 View 大小

android - Barrier 不动的原因

java - 将海拔应用到 TextInputLayout

java - 如何仅在 Android Wear 设备上显示通知

javascript - JQuery,单击复选框后值不会更新