简体   繁体   中英

Action Bar Search in collapsing Toolbar Layout

I have a CollapsingToolbarLayout with recycler view. In my actionbar I have a search menu item. But whenever I click search icon, the edittext layout do not appear.

Screenshots:- A) When search is not clicked 原来

B) When Search icon is clicked 点击后

My main_activity.xml code:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/activity_main_id"
    tools:context="objectdistance.ankeshkjaisansaria.ram.sita.cameratag.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/imagetoolbar"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>


    </android.support.design.widget.AppBarLayout>




    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



</android.support.design.widget.CoordinatorLayout>

In my activity.java the search item is handled this way:-

        ActionBar action = getSupportActionBar(); //get the actionbar
        action.setDisplayShowCustomEnabled(true); //enable it to display a
        // custom view in the action bar.
        action.setCustomView(R.layout.search_bar);//add the custom view
        action.setDisplayShowTitleEnabled(false); //hide the title

But the search_bar layout is not appearing only. And also the title is not hiding.

Is there any different way to handle search in menu item when using collapsing layout ?

Now we have toolbar in Android. So instead of ActionBar from Activity, use To Tooolbar view as ActionBar.

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();

Now operate on ab. By default your activity show a action bar but Toolbar is hiding it. Also use menu item as

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="Search for products"
     app:actionViewClass="android.support.v7.widget.SearchView"
     app:showAsAction="always|collapseActionView"
    />

You should implement something like this to get the "Search Field" working just fine:

   public boolean onCreateOptionsMenu(Menu menu) {

   getMenuInflater().inflate(R.menu.menu_search_list, menu);

  MenuItem searchMenuItem = menu.findItem(R.id.action_search);

  android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchMenuItem);

    searchView.setQueryHint(getString(R.string.type_here_to_filter));

    searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return searchForAnItemInTheRecyclerView(query);
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return searchForAnItemInTheRecyclerView(newText);
        }
    });

    return true;
}

尝试将此添加到菜单xml上的项目中。

app:actionLayout= "@layout/YourSearchLayout" 

Try to add the item like this in Menu.xml file:

 <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:icon="@drawable/src"
    android:title="@string/action_settings"
    app:showAsAction="always|collapseActionview" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM