Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - listview as action overflow in sherlock actionbar

How to do get a listdropdown on clicking sherlock action item.It should be similar to creating spinner. But I have a problem with that approach as I dont want the selected item to be shown on the actionbar.it should be similatr to action overflow.Can any on help me on this.Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create such behavior using Spinner (or IcsSpinner for ActionBarSherlock) in action layout of a menu item. Though you have to use a little trick - hide the currently selected item.

Create menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1"
          android:actionLayout="@layout/my_dropdown_action_layout"
          android:showAsAction="always"/>

Where res/layout-v14/my_dropdown_action_layout.xml will contain (this version is used for native action bar):

<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:background="?attr/actionBarItemBackground"
             android:id="@+id/spinner"/>

and res/layout/my_dropdown_action_layout.xml will contain (this version is used for ActionBarSherlock):

<?xml version="1.0" encoding="utf-8"?>
<com.actionbarsherlock.internal.widget.IcsSpinner 
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:background="?attr/actionBarItemBackground"
             android:id="@+id/spinner"/>

Using IcsSpinner is necessary to create a dropdown spinner. If you use res/layout-v14/my_dropdown_action_layout.xml layout for the default version (in res/layout/), it would behave differently on Android 2.x (the spinner would be in dialog mode).

Now you have to fill the spinner with data properly. Just create an Activity where you inflate the menu, this way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.my_menu, menu);
    MenuItem menuItem = menu.findItem(R.id.item1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, items);
    adapter.setDropDownViewResource(R.layout.list_item);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // native ActionBar
        Spinner sp = (Spinner) menuItem.getActionView();
        sp.setAdapter(adapter);
    } else {
        // ActionBarSherlock
        IcsSpinner sp = (IcsSpinner) menuItem.getActionView();
        sp.setAdapter(adapter);
    }

    return super.onCreateOptionsMenu(menu);
}

Now comes the trick of hiding the currently selected item. Layout res/layout/spinner_layout.xml will contain this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="@null">
    <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text"
            android:visibility="invisible"/>
    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/my_dropdown_icon"
            android:background="@null"/>
</FrameLayout>

This way you see an icon as the menu item and you have the dropdown menu. Note that layout res/layout/list_item.xml has to contain a TextView with id R.id.text too.

Alternatively, you can use similar approach where you can use ActionProvider instead of action layout.

And another solution would be to create custom widget similar to dropdown Spinner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...