先看效果图
实现方式:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView mTvSearch;
private RecyclerView mRec;
private List<Bean> mList = new ArrayList<>();
MyAdapter myAdapter;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
Bean bean = new Bean();
bean.name = "张三";
bean.age = 20;
bean.num = "30000";
Bean bean1 = new Bean();
bean1.name = "李四";
bean1.num = "40000";
bean1.age = 25;
mList.add(bean);
mList.add(bean1);
}
private void initView() {
mTvSearch = findViewById(R.id.tv_search);
mRec = findViewById(R.id.rec);
editText = findViewById(R.id.ed);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// myAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
myAdapter = new MyAdapter(mList,this);
mRec.setLayoutManager(new LinearLayoutManager(this));
mRec.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
myAdapter.appendList(mList);
mTvSearch.setOnClickListener(mOnClick);
}
private View.OnClickListener mOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_search:
myAdapter.getFilter().filter(editText.getText().toString().trim());
break;
default:
break;
}
}
};
}
适配器
package com.example.myapplication;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> implements Filterable {
private List<Bean> mSourceList = new ArrayList<>();
private List<Bean> mFilterList = new ArrayList<>();
private Context mContext;
public MyAdapter(List<Bean> mFilterList, Context mContext) {
this.mFilterList = mFilterList;
this.mContext = mContext;
}
public void appendList(List<Bean> list) {
mSourceList = list;
//这里需要初始化filterList
mFilterList = list;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
holder.tv_name.setText(mFilterList.get(position).name);
holder.tv_age.setText(mFilterList.get(position).num);
}
@Override
public int getItemCount() {
return mFilterList.size();
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String carString = constraint.toString();
if (TextUtils.isEmpty(carString)) {
// 没有过滤的内容就展示原数据
mFilterList = mSourceList;
} else {
List<Bean> filteredList = new ArrayList<>();
for (Bean bean : mSourceList) {
// 添加匹配规则
if (bean.name.contains(carString)) {
filteredList.add(bean);
}
}
mFilterList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilterList;
return filterResults;
}
// 过滤后的值返回来
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilterList = (List<Bean>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
static class MyHolder extends RecyclerView.ViewHolder {
TextView tv_name, tv_num, tv_age;
public MyHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
tv_age = itemView.findViewById(R.id.tv_age);
tv_num = itemView.findViewById(R.id.tv_num);
}
}
}