I'm looking for a way to filter my data in a RecyclerView without the state of a given view affects another view when filter is applied. Pratically, I have this problem :
1. I add "Martins" to my friend list by clicking on the add button (image msSxq.png)
list without filter
It'is then clear that, as the seraching action is done, my list content change and then the new items take old items place and take old items button state !
I'm looking for a way to avoid this. I tried to read several post on similar subject but nothing (Example of link : How to filter a RecyclerView with a SearchView)
Thank you for your helps !
Edit : 07/02/2020 - onBindViewHolder and Filtering Code
onBindViewHolder (in my Custom Adapter)
@Override
public void onBindViewHolder(@NonNull final FriendAdapterViewHolder holder, int position) {
String mUsernameString = suggestionList.get(position);
String mModUsernameString = suggestionList.get(position) + "_@chatter";
holder.username.setText(mUsernameString);
holder.mod_username.setText(mModUsernameString);
}
Filtering methods (always in my Adapter)
@Override
public Filter getFilter() {
return friendFilter;
}
private Filter friendFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<String> listFiltered = new ArrayList<>();
if (fullSuggestionList != null) {
if (constraint == null || constraint.length() == 0) {
listFiltered.addAll(fullSuggestionList);
} else {
String searchTextLowerCase = constraint.toString().toLowerCase().trim();
for (String item : fullSuggestionList) {
if (item.toLowerCase().contains(searchTextLowerCase)) {
listFiltered.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = listFiltered;
return results;
} else {
return null;
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values != null) {
suggestionList.clear();
suggestionList.addAll((ArrayList<String>) results.values);
notifyDataSetChanged();
}
}
};
And finally, in my Activity where I perform searching action
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (adapter.getCompleteItemList() != null) {
adapter.getFilter().filter(newText);
}
return false;
}
});