执行逻辑是这样的:
- 通过SearchView获取用户输入的文字.
- 把文字传到Adpater,在Adpater中对关键字进行筛选.
- 根据过滤后的结果刷新RecyclerView显示过滤后的界面.
先看布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_action"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.project.granden.enjoy.MyAction.ActionActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#bc4a4a">
<android.support.v7.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right">
</android.support.v7.widget.SearchView>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
一个Toolbar置于顶部里面包含一个SearchView,剩余的显示RecyclerView
然后看代码部分:
public class ActionActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
SearchView searchView;
Toolbar toolbar;
RecyclerView recyclerView;
MyAdpater adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action);
InitVIew();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<String>list=new ArrayList<>();
for (int i = 0; i < 15; i++) {
list.add(String.valueOf(i));
}
adpter=new MyAdpater(list);
recyclerView.setAdapter(adpter);
}
private void InitVIew() {
recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
toolbar= (Toolbar) findViewById(R.id.toolbar);
searchView= (SearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adpter.getFilter().filter(newText);
return false;
}
}
主要看Activity实现的接口:SearchView.OnQueryTextListener
实现该接口后覆写两个方法它们的作用就是对SearchView的输入框进行监听:
- onQueryTextSubmit 当完成输入的内容点击搜索按钮后该方法会回调,参数String query返回当前文本框可见的文字
- onQueryTextChange 每次当文本框的内容发生改变该方法会回调,参数String newText返回当前文本框可见的文字
可以看到我们在onQueryTextChange()方法中调用了adpter.getFilter().filter(newText);
他的作用就是对Adpater的内容进行过滤.
每当SearchView的输入框内容发生变化就把输入框的内容传送到Adpater的过滤器接着进行一系列的操作.
public class MyAdpater extends RecyclerView.Adapter<MyAdpater.ViewHolder> implements Filterable {
List number,temp_number;
TestFilter myFilter;
public MyAdpater(List<String> number) {
this.number=number;
temp_number = number;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.number.setText((String) number.get(position));
}
@Override
public int getItemCount() {
return number.size();
}
@Override
public Filter getFilter() {
if (myFilter == null) {
myFilter = new TestFilter();
}
return myFilter;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView number;
public ViewHolder(View itemView) {
super(itemView);
number = (TextView) itemView.findViewById(R.id.number);
}
}
class TestFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<String> new_number=new ArrayList();
if (constraint != null && constraint.toString().trim().length() > 0) {
for (int i = 0; i < temp_number.size(); i++) {
String content = (String) temp_number.get(i);
if (content.contains(constraint)) {
new_number.add(content);
}
}
}else {
new_number=temp_number;
}
FilterResults filterResults = new FilterResults();
filterResults.count = new_number.size();
filterResults.values = new_number;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//这里对number进行过滤后重新赋值
number = (List) results.values;
//如果过滤后的返回的值的个数大于等于0的话,对Adpater的界面进行刷新
if (results.count > 0) {
notifyDataSetChanged();
} else {
//否则说明没有任何过滤的结果,直接提示用户"没有符合条件的结果"
umber = new ArrayList(){};
number.add(": ( 没有符合条件的结果");
notifyDataSetChanged();
}
}
}
}
来看看Adpater的内部:
- MyAdpater(List number)构造方法接收一个List参数
- 并且把number赋值给了temp_number(原因后面会解释)
- 细心的同学可能早就看到了我们实现了Filterable接口,这个方法需要覆写getFilter()方法.
public Filter getFilter() {
if (myFilter == null) {
myFilter = new TestFilter();
}
return myFilter;
}
方法需要返回Filter,所以我们新建一个TestFilter类继承至Filter
class TestFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<String> new_number=new ArrayList();
if (constraint != null && constraint.toString().trim().length() > 0) {
for (int i = 0; i < temp_number.size(); i++) {
String content = (String) temp_number.get(i);
if (content.contains(constraint)) {
new_number.add(content);
}
}
}else {
new_number=temp_number;
}
FilterResults filterResults = new FilterResults();
filterResults.count = new_number.size();
filterResults.values = new_number;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
number = (List) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
number.add(": ( 没有符合条件的结果");
notifyDataSetChanged();
}
}
}
Filter类里面有两个方法:
performFiltering:参数constraint是adpter.getFilter().filter(newText)方法传入的newText(也就是过滤的条件)
该方法主要完成对数据进行过滤的工作
publishResults:参数CharSequence,results第一个参数这里不需要用到,第二个参数是上面的performFiltering返回的值.
在该方法里面进行界面的刷新工作
里面实现的思路是:
- 新建一个List new_number这个对象用来存储过滤后符合条件的值
- 判断constraint是否为空,是否有值.满足条件的话遍历List,并且判断List的值是否有包含过滤的条件.如果是的话把值存到new_number.如果一个符合的都没有把temp_number的值(也就是最开始传进来的List)赋值给new_number
- 新建一个FilterResults对象分别把new_number的size和value赋值给FilterResults.value和FilterResults.count,然后返回FilterResults对象
- 接着在publishResults()方法中取出过滤后满足条件的值也就是results.values
- 判断results.count的过滤后结果的个数是否大于0
- 大于0的话把值赋值给number然后刷新界面,不满足的话把temp_number(也就是最开始传进来的值)赋值给number,然后刷新界面