使用edit text和recyclerview实现一个搜索功能
时间: 2024-03-24 18:35:58 浏览: 168
RecyclerView+EditText 动态搜索数据
好的,下面是一个简单的使用EditText和RecyclerView实现搜索功能的示例:
1. 首先,在你的布局文件中添加一个EditText和一个RecyclerView,例如:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
```
2. 在你的Activity或Fragment中,初始化EditText和RecyclerView,并为EditText添加文本改变监听器:
```java
public class MyActivity extends AppCompatActivity {
private EditText searchBox;
private RecyclerView recyclerView;
private MyAdapter adapter;
private List<String> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
searchBox = findViewById(R.id.search_box);
recyclerView = findViewById(R.id.recycler_view);
// 初始化数据
dataList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
dataList.add("Item " + i);
}
// 初始化适配器和RecyclerView
adapter = new MyAdapter(dataList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
// 添加文本改变监听器
searchBox.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) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {}
});
}
}
```
3. 创建一个自定义Adapter,并实现Filterable接口,用于过滤数据:
```java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements Filterable {
private List<String> dataList;
private List<String> filteredDataList;
public MyAdapter(List<String> dataList) {
this.dataList = dataList;
this.filteredDataList = new ArrayList<>(dataList);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText(filteredDataList.get(position));
}
@Override
public int getItemCount() {
return filteredDataList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String searchText = constraint.toString().toLowerCase(Locale.getDefault());
List<String> tempList = new ArrayList<>();
if (searchText.isEmpty()) {
tempList.addAll(dataList);
} else {
for (String item : dataList) {
if (item.toLowerCase(Locale.getDefault()).contains(searchText)) {
tempList.add(item);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = tempList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredDataList.clear();
filteredDataList.addAll((List<String>) results.values);
notifyDataSetChanged();
}
};
}
static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
}
}
}
```
以上就是使用EditText和RecyclerView实现搜索功能的一个简单示例,你可以根据自己的需求进行修改和扩展。
阅读全文