当用户单击回收器行时,在工具栏中停止SearchView

时间:2018-02-22 14:39:23

标签: android android-recyclerview android-toolbar searchview

我正在尝试在具有NaviagtionDrawer的工具栏中实现SearchView,以便在用户单击搜索图标时仅显示RecyclerView。然后,当用户在RecyclerView中选择一行时,我希望RecyclerView消失并且searchView会崩溃。当搜索没有完成时,应用程序的主要功能正在发生。

我已经使搜索过滤工作,并且适配器中的onClick()完成了它的工作。但是,我可以保持RecyclerView不占用屏幕的唯一方法是调用RecyclerView.setAdapter()中的onOptionsItemSelected()方法,这种方法可能不对。但是,我无法将其关闭,因此这是一次性解决方案;一旦上升,它就会熬夜!

这是我的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/base_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:context="com.lampreynetworks.cpa.CPA_Activity"
          android:orientation="vertical">

<include
    android:id="@+id/toolbar_search"
    layout="@layout/toolbar_search_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/borderLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/text_view_border"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_connect_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:text="Please take a reading now with the medical device you have connected to your CareSpan PHG app. Be sure to select the right patient from the list above."
                android:textColor="@color/orange"
                android:textStyle="bold"/>
        </RelativeLayout>

        <ListView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/borderLayout"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:choiceMode="none"
            android:stackFromBottom="false">
        </ListView>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_drawer_menu"/>
</android.support.v4.widget.DrawerLayout>

以下是包含工具栏

的包含的布局
<?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"                                                 
    tools:context="com.lampreynetworks.cpa.CPA_Activity">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_search_id"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorToolbarBackground"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp">
    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?actionBarSize"
        android:scrollbars="vertical"/>

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


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

我有RecyclerView行的xml(使用Cardview),它应该显示。

所以基本问题是

  • 如何在用户点击工具栏之前隐藏RecyclerView 搜索图标和
  • 当用户点击某行时,如何重新隐藏它 显示已过滤项目的RecyclerView?

0 个答案:

没有答案