首页 > 解决方案 > 回收站视图中的复选框

问题描述

我在使用带有回收站视图的复选框时遇到了问题。问题是当我检查第二个元素时,列表底部的元素也会被选中,当我选择任何其他元素时也会发生类似的情况。我搜索了stackoverflow,发现了类似的问题和答案,我尝试了这些解决方案,但没有奏效。

适配器类:

public class addServicesCheckOutAdapter extends RecyclerView.Adapter<addServicesCheckOutAdapter.ViewHolder>  implements Filterable {

private List<ServiceList> listitemS;
private  List<ServiceList> listitemsFilter;
private Context context;

private Map<String,String> hashMap;
private ArrayList<String> pricesList;
private List<String> list;
private int prevSelection = -1;
private int selectedPosition;
private boolean var = false;
Set<Integer> mCheckedItemPositions = new HashSet<>();


public addServicesCheckOutAdapter(List<ServiceList> listitems, Context context) {
    this.listitemS = listitems;
    this.context = context;
    this.listitemsFilter = listitems;
}

public  void filteredList(List<ServiceList> filetList) {
    listitemS = filetList;
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view  = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_add_services,parent,false);

    pricesList = new ArrayList<>();
    hashMap = new ConcurrentHashMap<>();
    list = new ArrayList<>();


    global.addServicesPrices = pricesList;
    global.addServicesNameAndID = hashMap;

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    final ServiceList listitem = listitemsFilter.get(position);
    holder.tvServiceName.setText(listitem.getService_name());
}

@Override
public int getItemCount() {
    return listitemsFilter.size();
}

@Override
public Filter getFilter() {
    return  new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String Key = constraint.toString();

            if (Key.isEmpty()){
                listitemsFilter = listitemS;
            }
            else {
                List<Listitem> IstFilter = new ArrayList<>();
            }
            FilterResults  filterResults = new FilterResults();
            filterResults.values= listitemsFilter;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            notifyDataSetChanged();
        }
    };
}

public void filterList(ArrayList<ServiceList> filterdNames) {
    this.listitemS = filterdNames;
    notifyDataSetChanged();
}

public static class ViewHolder extends RecyclerView.ViewHolder{

    TextView tvServiceName;
    CheckBox checkBox;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        tvServiceName = itemView.findViewById(R.id.tvServiceName);
        checkBox = itemView.findViewById(R.id.checkBox);
    }




}//View Holder

}

标签: androidandroid-recyclerview

解决方案


在您的适配器中设置以下方法

@Override
 public long getItemId(int position) 
 {
        return position;
 }

 @Override
 public int getItemViewType(int position) 
 {
        return position;
 }

希望这可以帮助你。


推荐阅读