最近在一个项目中使用到了搜索功能,特来此记录一下
本篇博文需要用到的知识点:
1.RecyclerView的简单操作
2.本地数据库的简单操作
3.ScrollView与RecyclerView的滑动冲突解决
废话不多说,先上效果图
先看看工程界面截图
其中HistorySearchAdapter是历史记录的适配器,HistorySearchUtil是本地数据库的管理工具类
1.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="zzx.historysearch.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:paddingTop="4dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="4dp">
<EditText
android:id="@+id/search_edit"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content"
android:hint="搜索"
android:textColor="#333333"
android:textColorHint="#dddddd"
android:textSize="15sp"/>
<TextView
android:id="@+id/search_tv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="搜索"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/history_search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="历史纪录"
android:textColor="#dddddd"
android:textSize="13sp"
android:paddingTop="8dp"
android:paddingLef