过滤器列表正确填充过滤后的元素,即使在notifydatasetchanged之后,text.setText()正确地填充元素,但在实际设备上元素都是错误的,有人可以解释一下吗?
代码和调试截图如下:
RecyclerAdapter:
public class PropertyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<PropertyModal> propertyModals, propertyModalFilterList;
private LayoutInflater inflater;
private PropertyItemBinding propertyItemBinding;
public PropertyAdapter(Context context, List<PropertyModal> propertyModals) {
this.inflater = LayoutInflater.from(context);
this.propertyModals = propertyModals;
this.propertyModalFilterList = new ArrayList<>();
this.propertyModalFilterList.addAll(this.propertyModals);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.property_item, parent, false);
return new PropertyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
PropertyModal property = propertyModalFilterList.get(position);
if (position % 2 == 0){
propertyItemBinding.propertyItemLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else {
propertyItemBinding.propertyItemLayout.setBackgroundColor(Color.parseColor("#F9F9F9"));
}
propertyItemBinding.propertyTitle.setText(property.getPropertyName());
}
@Override
public int getItemCount() {
return (null != propertyModalFilterList ? propertyModalFilterList.size() : 0);
}
class PropertyViewHolder extends RecyclerView.ViewHolder{
public PropertyViewHolder(View itemView) {
super(itemView);
propertyItemBinding = PropertyItemBinding.bind(itemView);
}
}
public void filter(final String text) {
propertyModalFilterList.clear();
if (TextUtils.isEmpty(text)) {
propertyModalFilterList.clear();
propertyModalFilterList.addAll(propertyModals);
} else {
for (PropertyModal item : propertyModals) {
if (item.getPropertyName().toLowerCase().contains(text.toLowerCase())) {
propertyModalFilterList.add(item);
}
}
}
notifyDataSetChanged();
}
}
MainFragment:
propertyBinding.searchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence query, int start, int before, int count) {
adapter.filter(query.toString());
//propertyBinding.propertyList.scrollToPosition(0);
}
@Override
public void afterTextChanged(Editable s) {
}
});