Android ActionBar/ActionBarSherlock multiple choices spinner
I have a ListView containing items which belongs to one or more category. I would like, by clicking on an icon in the actionbar, to select and unselect theses categories. This way, the listView is refresh according to the categories selected.
Here is an example I found :
http://www.hostingpics.net/viewer.php?id=581753Screenshot20140110103007.png
For the moment, I found 2 solutions :
The second solution fits exactly with the UI expectations but I think there is a sort of multiple choices spinner solution.
A Spinner
shows the drop down using a ListPopupWindow
, you could use the same to show that multi choice item selection list:
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. There are two other things you need to take care:
The mAnchor
is a View
that you need to insert in the Activity
's layout in the top-right corner so the ListPopupWindow
will show in the right position. For example, if you have as an Activity
root:
a RelativeLayout
then mAnchor
will be:
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
a LinearLayout
then mAnchor
will be:
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)
and so on for other types of layouts.
Secondly, you need to setup the width of the ListPopupWindow
to a desired value. You'll need to adapt this value for different screen sizes and orientation(like phone-portrait and phone-landscape, different table sizes in portrait and landscape).
Original guide available at http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
Adding Drop-down Navigation
Figure 9. A drop-down navigation list in the action bar.
As another mode of navigation (or filtering) for your activity, the action bar offers a built in drop-down list (also known as a "spinner"). For example, the drop-down list can offer different modes by which content in the activity is sorted.
Using the drop-down list is useful when changing the content is important but not necessarily a frequent occurrence. In cases where switching the content is more frequent, you should use navigation tabs instead.
The basic procedure to enable drop-down navigation is:
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);This method takes your SpinnerAdapter and ActionBar.OnNavigationListener.
This procedure is relatively short, but implementing the SpinnerAdapter and ActionBar.OnNavigationListener is where most of the work is done. There are many ways you can implement these to define the functionality for your drop-down navigation and implementing various types of SpinnerAdapter is beyond the scope of this document (you should refer to the SpinnerAdapter class reference for more information). However, below is an example for a SpinnerAdapter and ActionBar.OnNavigationListener to get you started (click the title to reveal the sample).
including the ListView in a RelativeLayout and then having View.GONE set until the user pressed the button in which you wish to show seems like a sensible way to do it. Using a ListViewAdapter you can populate the list of items.
Each Item could be a linear layout of horizontal orientation.
Hope my suggestion helps! :)
链接地址: http://www.djcxy.com/p/18276.html上一篇: 尝试在Android中缩小位图不起作用