在日常开发中,我们会遇到一种需求,就是通过输入关键字快速的查询当前列表中的数据并进行过滤显示。(感觉好难用文字描述这个功能啊·····),在网上找了一些资料也没有类似的文章,只好自己变尝试变查资料,用了几个小时的时间终于搞出来了。
OK,看图:
1.gif
组件准备
1.SearchView:SearchView是Android原生的搜索框控件,它提供了一个用户界面,用于用户搜索查询。
SearchView默认是展示一个search的icon,点击icon展开搜索框,如果你想让搜索框默认就展开,可以通过setIconifiedByDefault(false);实现。
2.XRecyclerView: github地址
这个组件主要是对RecyclerView的封装,主要完成了下拉刷新、上拉加载更多、RecyclerView头部。
关于这个组件的详解,请参考这篇文章:文章地址
开始布局
现在先进行布局:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/window_background"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true">
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_main_color"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:title="@string/location_detail"
app:titleTextColor="@color/white" />
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:iconifiedByDefault="false"
android:queryHint="@string/search_hint"
android:textColor="@color/white"
/>
android:id="@