Android ActionBar / ActionBarSherlock multiple rotation options

I have a ListView item that belongs to one or more categories. I would like to select and deselect abstract categories by clicking on the icon in the action bar. Thus, the listView is updated according to the selected categories.

Here is an example I found:

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

At the moment, I have found 2 solutions:

  • Adding a counter with checked items, but it closes the menu on each unselection
  • Create a ListView with cheackable elements in a RelativeLayout and make it appear when you click the icon.

The second solution exactly matches UI expectations, but I think there are several spinner solution options.

+7
android checkbox actionbarsherlock listview spinner
source share
3 answers

A Spinner shows a drop-down menu using a ListPopupWindow , you can use it to display a selection list of select items:

 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 `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 can then call this method from the onOptionsItemSelected() callback when the correct MenuItem selected. You need to take care of two things:

mAnchor is a View that you need to insert into the Activity layout in the upper right corner so that ListPopupWindow displayed in the correct position. For example, if you have 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) 

etc. for other types of layouts.

Secondly, you need to adjust the width of the ListPopupWindow to the desired value. You will need to adapt this value for different screen sizes and orientations (for example, a portrait of a phone and a landscape phone, different table sizes in portrait and landscape orientation).

+2
source share

The original guide is available at http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

Adding Dropdown Navigation


Image

Figure 9. Navigation drop-down list in the action bar.

As another navigation (or filtering) mode for your activity, the action bar offers a built-in drop-down list (also known as a "spinner"). For example, a drop-down list may offer different modes by which content in an operation is sorted.

Using the drop-down list is useful when changing content is important, but not necessarily frequent. In cases where content switching happens more often, you should use the navigation tabs instead.

The basic procedure for enabling the drop-down menu:

actionBar.setListNavigationCallbacks (mSpinnerAdapter, mNavigationCallback); this method accepts a SpinnerAdapter and an ActionBar.OnNavigationListener .

This procedure is relatively short, but implemented by SpinnerAdapter and ActionBar.OnNavigationListener , where most of the work is done. There are many ways to implement them to determine the functionality for the drop-down list of navigation and implement various types of SpinnerAdapter beyond the scope of this document (refer to SpinnerAdapter for more information). However, the following is an example of the SpinnerAdapter and ActionBar.OnNavigationListener to get started (click the title to open the sample).

+1
source share

including the ListView in RelativeLayout and then setting View.GONE until the user clicks the button in which you want to show seems like a reasonable way to do this. With the ListViewAdapter you can populate a list of items.

Each element can be a linear arrangement of horizontal orientation.

Hope my suggestion helps! :)

0
source share

All Articles