Android ActionBar: collapsible SearchView with action button

前端 未结 4 1896
孤城傲影
孤城傲影 2020-12-13 14:29

How to build ActionBar with collapsible search view with single action item visible, when search view is expanded? To be more descriptive, this is what I need:

相关标签:
4条回答
  • 2020-12-13 15:14

    Just a change menu xml. showaction prefix to your app name

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" >
    
    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/actionbar_search"
        android:icon="@drawable/action_search"
        app:showAsAction="always|collapseActionView"
        android:title="@string/search"
    />
    
    </menu>
    
    0 讨论(0)
  • 2020-12-13 15:22

    After considering the suggestions from comments, many googling and testing succeeded in obtaining the desired effect. I'm not proud of this solution but it works, it's not too complicated and should be enough for this case.

    In resume what I did:

    1. Placed SearchView with custom "add item" button in customView of ActionBar.
    2. Removed collapseActionView and android:actionLayout from search item in menu.xml
    3. Collapsing/expanding SearchView programmatically.

    Some code to better understand what I did. Maybe this can be useful for someone.

    menu.xml

    <item
        android:id="@+id/menu_search"
        android:icon="@drawable/action_search"
        android:showAsAction="always"
        android:title="@string/search" />
    
    // ... other menu items
    

    actionbar_search.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="horizontal">
    
        <com.actionbarsherlock.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:iconifiedByDefault="false"
            android:queryHint="@string/search_hint"
            android:visibility="invisible" />
    
        <ImageButton
            android:id="@+id/add_item"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            style="@style/Widget.Sherlock.ActionButton"
            android:src="@drawable/content_new"
            android:title="@string/add_item" />
    
    </LinearLayout>
    

    MainActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        actionBar.setCustomView(R.layout.actionbar_search);
        actionBarCustomView = ab.getCustomView();
        searchView = ((SearchView) actionBarCustomView.findViewById(R.id.search_view));
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                searchView.setVisibility(View.INVISIBLE);
                return false;
            }
        });
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // ...
            case R.id.menu_search:
                if (searchView.getVisibility() == View.VISIBLE) {
                    searchView.setIconified(true);
                    searchView.setVisibility(View.INVISIBLE);
                } else {
                    searchView.setMaxWidth(abCustomView.getWidth() - addButton.getWidth());
                    searchView.setVisibility(View.VISIBLE);
                    searchView.setIconified(false);
                }
                break;
            // ...
        }
        return true;
    }
    
    @Override
    public void onBackPressed() {
    
        if (searchView.getVisibility() == View.VISIBLE) {
            searchView.setIconified(true);
            searchView.setVisibility(View.INVISIBLE);
            return;
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-12-13 15:25

    You can do it only using yourmenu.xml.

    Set your other menu icon to always show as an action. Your menu.xml should look like this:

    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/actionbar_search"
        android:icon="@drawable/action_search"
        android:showAsAction="always|collapseActionView"
        android:title="@string/search"
    />
    <item
        android:id="@+id/your_other_menu_id"
        android:showAsAction="always"
        android:icon="@android:drawable/your_other_menu_ic"
     />
    
    0 讨论(0)
  • 2020-12-13 15:31

    Simply you can do it like this

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.main, menu);
         MenuItem searchItem = menu.findItem(R.id.action_search);
         searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
         searchView.setIconified(true); //to be opened collapsed
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
    
            case R.id.action_search:
                searchView.setIconified(false);// to Expand the SearchView when clicked
                return true;
        }    
        return false;
    }
    

    And at search item in menu.xml make

    showAsAction="always"

    Note: I am using android.support.v7.widget.SearchView but in your case it doesn't matter

    0 讨论(0)
提交回复
热议问题