简体   繁体   English

展开的工具栏布局中的操作栏搜索

[英]Action Bar Search in collapsing Toolbar Layout

I have a CollapsingToolbarLayout with recycler view. 我有一个带有回收站视图的CollapsingToolbarLayout。 In my actionbar I have a search menu item. 在我的操作栏中,我有一个搜索菜单项。 But whenever I click search icon, the edittext layout do not appear. 但是,每当我单击搜索图标时,edittext布局都不会出现。

Screenshots:- A) When search is not clicked 截图:-A)未单击搜索时 原来

B) When Search icon is clicked B)当点击搜索图标时 点击后

My main_activity.xml code:- 我的main_activity.xml代码:-

<?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:- 在我的activity.java中,搜索项是通过以下方式处理的:

        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. 但是search_bar布局不只出现。 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. 现在我们有了Android中的工具栏。 So instead of ActionBar from Activity, use To Tooolbar view as ActionBar. 因此,请使用To Tooolbar视图作为ActionBar,而不是Activity中的ActionBar。

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

Now operate on ab. 现在对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: 尝试在Menu.xml文件中添加这样的项目:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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