SearchView searchView = findViewById(R.id.searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// Create a new list to store the filtered strings
List<String> filteredList = new ArrayList<>();
// Loop through the original list of strings and add only those
// that contain the search query to the filtered list
for (String s : originalList) {
if (s.toLowerCase().contains(newText.toLowerCase())) {
filteredList.add(s);
}
}
// Update the RecyclerView with the filtered list
recyclerView.setAdapter(new YourAdapter(filteredList));
return false;
}
});
1条答案
按热度按时间jyztefdp1#
在活动类中,初始化SearchView并设置OnQueryTextListener以侦听查询文本的更改:
希望这能帮上忙。