Nim*_*ili 5 android searchview android-recyclerview android-livedata android-viewmodel
我有一个RecyclerView。我想用 SearchView 让它可以过滤。我的应用程序基于Android Architecture Components.
我创建了一个过滤方法searchView。当我搜索某些内容时,它可以工作,但是当我清除其中的文本时searchView,列表不会更改,并且仍然显示搜索数据。
活动:
private StudentAdapter mAdapter;
mAdapter = new StudentAdapter(this);
recyclerView.setAdapter(mAdapter);
//ViewModel
StudentViewModelFactory factory = new StudentViewModelFactory(mDb, mClassId);
StudentViewModel viewModel = ViewModelProviders.of(this, factory).get(StudentViewModel.class);
viewModel.getStudentEntries().observe(this, studentEntries -> mAdapter.setStudents(studentEntries));
//SearchView
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query){
return false;
}
@Override
public boolean onQueryTextChange(String newText){
mAdapter.getFilter().filter(newText);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
适配器:
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.itemHolder> {
private List<StudentEntry> mStudentEntries;
private List<StudentEntry> filteredStudentEntries;
public StudentAdapter(Context context) {
this.context = context;
}
public void setStudents(List<StudentEntry> studentEntries) {
this.mStudentEntries = studentEntries;
notifyDataSetChanged();
}
public void setPerformances(List<PerformanceEntry> performanceEntries) {
this.mPerformanceEntries = performanceEntries;
notifyDataSetChanged();
}
.
.
.
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mStudentEntries = (List<StudentEntry>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredStudentEntries = null;//avaz shod vali test nashod
if (constraint.length() == 0) {
filteredStudentEntries = mStudentEntries;
} else {
filteredStudentEntries = getFilteredResults(constraint.toString().toLowerCase());
}
FilterResults results = new FilterResults();
results.values = filteredStudentEntries;
return results;
}
};
}
protected List<StudentEntry> getFilteredResults(String constraint) {
List<StudentEntry> results = new ArrayList<>();
for (StudentEntry item : mStudentEntries) {
if (item.getStudentName().toLowerCase().contains(constraint)
|| item.getStudentId().toLowerCase().contains(constraint)) {
results.add(item);
}
}
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
改变 :
private List<StudentEntry> filteredStudentEntries;
Run Code Online (Sandbox Code Playgroud)
到 :
private ArrayList<StudentEntry> arrayList = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
和改变:
public void setStudents(List<StudentEntry> studentEntries) {
this.mStudentEntries = studentEntries;
notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
到 :
public void setStudents(List<StudentEntry> studentEntries) {
this.mStudentEntries = studentEntries;
arrayList.addAll(mStudentEntries);
notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
并更改过滤器方法:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase().trim();
mStudentEntries.clear();
if (constraint.length() == 0) {
mStudentEntries.addAll(arrayList);
} else {
for (StudentEntry item : arrayList) {
if (item.getStudentName().toLowerCase(Locale.getDefault()).contains(constraint)
|| item.getStudentId().toLowerCase(Locale.getDefault()).contains(constraint)) {
mStudentEntries.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = mStudentEntries;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
notifyDataSetChanged();
}
};
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记Filterable在适配器类中实现。
| 归档时间: |
|
| 查看次数: |
2173 次 |
| 最近记录: |