列表视图滚动时隐藏搜索栏

时间:2017-12-03 07:10:31

标签: java android listview scroll

如何在listView中添加搜索栏以在滚动时隐藏它?

enter image description here

这里是我的代码xml:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">


    <SearchView
        android:id="@+id/maBtnSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:iconifiedByDefault="false"
        android:queryHint="Search User" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView">
    </ListView>

</LinearLayout>

这是我上面的完整布局代码,谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

试试这个。 searchview放在工具栏上。根据您的使用情况更改代码。

 <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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:background="@drawable/back"
        android:id="@+id/pager_sessions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.view.ViewPager>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/search_edit_frame"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_sessions"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways">
            <android.support.v7.widget.SearchView
                android:id="@+id/search_item"
                android:layout_gravity="right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </android.support.v7.widget.SearchView>
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            app:layout_scrollFlags="scroll|enterAlways"
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabIndicatorColor="@android:color/background_light"
            app:tabSelectedTextColor="@android:color/background_light"
            app:tabTextColor="@android:color/background_light">
        </android.support.design.widget.TabLayout>

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