版权声明:本文为xing_star原创文章,转载请注明出处!
本文同步自http://javaexception.com/archives/82
做了很多年的app开发,貌似没见过没有搜索功能的,搜索这个功能还真是挺常见的,一般包括本地搜索,比如笔记类的,有道云笔记,或者Leanote,或者是qq,微信这样的IM软件,搜索本地聊天数据。也有一些是根据关键字,发起http请求,让服务端搜索。在本文主要关注的是本地搜索,比如搜索数据库中的内容,或者是其他本地存储的内容。
通常会觉得写搜索页面,比较麻烦,至少我是这么觉得的,关于RecyclerView跟SearchView准备多写几篇,这篇就讲最基础的使用方式,下篇会讲在SearchView在Toolbar中的场景,也就是点击搜索按钮,进入搜索页面自动展开搜索栏。
xml布局文件
<?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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.SearchView android:id="@+id/search_view" android:layout_width="match_parent" app:queryHint="搜索" android:layout_height="wrap_content" /> <View android:layout_width="match_parent" android:background="#dddddd" android:layout_height="1px"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_marginTop="2dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
RecyclerView设置的Adapter类
class Adapter extends RecyclerView.Adapter { private List<String> datas = new ArrayList<>(); public void setDatas(List<String> datas) { this.datas = datas; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { ViewHolder holder = (ViewHolder) viewHolder; holder.textView.setText(datas.get(position)); } @Override public int getItemCount() { return datas.size(); } } class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(android.R.id.text1); } }
RecyclerView item的布局文件用的是Android系统提供的一个比较简单的布局,android.R.layout.simple_list_item_1。
对SearchView设置监听事件
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { adapter.setDatas(datas); adapter.notifyDataSetChanged(); } else { adapter.setDatas(search(newText)); adapter.notifyDataSetChanged(); } return false; } });
由于是本地搜索,希望的效果是可以实时搜索,每输入一个字符,就重新搜索一次,于是将搜索事件放到了onQueryTextChange()方法中,如果不希望实时搜索,可以将过滤数据源的逻辑放到onQueryTextSubmit()方法中,当软键盘弹起的时候,我们会看到软键盘上右下角是一个搜索的按钮。点击此按钮就会触发一次搜索。
完整的java代码如下:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private SearchView searchView; private RecyclerView recyclerView; private Adapter adapter; private List<String> datas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchView = findViewById(R.id.search_view); recyclerView = findViewById(R.id.recycler_view); adapter = new Adapter(); datas = generateDatas(); adapter.setDatas(datas); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { adapter.setDatas(datas); adapter.notifyDataSetChanged(); } else { adapter.setDatas(search(newText)); adapter.notifyDataSetChanged(); } return false; } }); } private List<String> search(String query) { List<String> filterDatas = new ArrayList<>(); for (String source : datas) { if (source.contains(query)) { filterDatas.add(source); } } return filterDatas; } private List<String> generateDatas() { List<String> list = new ArrayList<>(); for (int index = 0; index < 10; index++) { list.add("Hello World " + (index + 1)); } return list; } class Adapter extends RecyclerView.Adapter { private List<String> datas = new ArrayList<>(); public void setDatas(List<String> datas) { this.datas = datas; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { ViewHolder holder = (ViewHolder) viewHolder; holder.textView.setText(datas.get(position)); } @Override public int getItemCount() { return datas.size(); } } class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(android.R.id.text1); } } }
链接:https://pan.baidu.com/s/1HcSUpfLBcWuXyHJ7vOtCxg 密码:89sa
Android RecyclerView SearchView基本用法1
原文:https://www.cnblogs.com/xing-star/p/10826829.html