我有一个带有searchview的recyclerview。
在每一行中,我都为多个选择添加了复选框。但是当我想搜索某些内容时,我会检查一些行。当我返回所有行时,位置混合在一起。
我在适配器中添加了getItemViewType和getItemId,但是什么也没发生
过滤recyclerview时我得到了错误的位置
MyAdapter类
public class ExpandableListSearchAdapter extends RecyclerView.Adapter<ExpandableListSearchAdapter.MyViewHolder> implements Filterable {
private List<RowModel> contactList;
private List<RowModel> contactListFiltered;
private Context context;
private int type;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textName;
public AppCompatCheckBox checkBox1;
MyViewHolder(View view) {
super(view);
textName = view.findViewById(R.id.textName);
checkBox1 = view.findViewById(R.id.checkBox1);
}
}
ExpandableListSearchAdapter(Context context, List<RowModel> contactList, int type) {
this.context = context;
this.contactList = contactList;
this.type = type;
this.contactListFiltered = contactList;
}
@NonNull
@Override
public ExpandableListSearchAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_searched_recycler_checkbox, viewGroup, false);
return new ExpandableListSearchAdapter.MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ExpandableListSearchAdapter.MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final RowModel contact = contactListFiltered.get(position);
holder.textName.setText(contact.getText());
holder.checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkItem(position, holder);
}
});
}
private void checkItem(int position, MyViewHolder holder) {
RowModel rm = contactListFiltered.get(position);
switch (type) {
case 1:
if (holder.checkBox1.isChecked()) {
holder.checkBox1.setChecked(true);
UsedDatasSearch.addToListSearchedComputer(rm);
}else{
holder.checkBox1.setChecked(false);
UsedDatasSearch.removeToListSearchedComputer(rm);
}
break;
case 2:
if (holder.checkBox1.isChecked()) {
holder.checkBox1.setChecked(true);
UsedDatasSearch.addToListSearchedComputerCpu(rm);
}else{
holder.checkBox1.setChecked(false);
UsedDatasSearch.removeToListSearchedComputerCpu(rm);
}
break;
}
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return contactListFiltered.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
contactListFiltered = contactList;
} else {
List<RowModel> filteredList = new ArrayList<>();
for (RowModel row : contactList) {
if (row.getText().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
contactListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = contactListFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
contactListFiltered = (ArrayList<RowModel>) filterResults.values;
notifyDataSetChanged();
}
};
}
}
逐步截屏
当我过滤某行
毕竟我重置了过滤器
答案 0 :(得分:0)
尝试使您的持有人最终身份,然后编写以下代码:
holder.checkBox1.setTag(position);
holder.checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int)view.getTag();
checkItem(pos, holder);
}
});
将已过滤的联系人列表替换为联系人列表:
private void checkItem(int position, MyViewHolder holder) {
RowModel rm = contactList.get(position);