1MA*_*GIN 7 checkbox android listview spinner actionbarsherlock
我有一个ListView包含属于一个或多个类别的项目.我想通过单击操作栏中的图标来选择和取消选择这些类别.这样,listView将根据所选类别进行刷新.
这是我发现的一个例子:
http://www.hostingpics.net/viewer.php?id=581753Screenshot20140110103007.png
目前,我找到了两个解决方案:
第二个解决方案完全符合用户界面的期望,但我认为spinner解决方案有多种选择.
ASpinner
使用 a 显示下拉菜单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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在选择onOptionsItemSelected()
右侧时从回调中调用此方法。MenuItem
您还需要注意另外两件事:
mAnchor
您需要将其View
插入到Activity
右上角的布局中,以便ListPopupWindow
将显示在正确的位置。例如,如果您有根Activity
:
RelativeLayout
那么amAnchor
将是:
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
Run Code Online (Sandbox Code Playgroud)
LinearLayout
那么amAnchor
将是:
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)
Run Code Online (Sandbox Code Playgroud)
对于其他类型的布局依此类推。
其次,您需要将宽度设置ListPopupWindow
为所需的值。您需要根据不同的屏幕尺寸和方向(例如手机纵向和手机横向、纵向和横向的不同表格尺寸)调整此值。