android - 回收站视图中的复选框
问题描述
我在使用带有回收站视图的复选框时遇到了问题。问题是当我检查第二个元素时,列表底部的元素也会被选中,当我选择任何其他元素时也会发生类似的情况。我搜索了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
}
解决方案
在您的适配器中设置以下方法
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getItemViewType(int position)
{
return position;
}
希望这可以帮助你。
推荐阅读
- node.js - 从 Angular 发布到节点。在节点上获取空体
- html - 在 HTML 中为浏览器呈现(和重新呈现)更快的是:表格或 flex-box
- linux - 是否可以使用 pybluez“授权服务”?
- midi - Audiokit MIDI 线程安全
- android - 设置项目 Android Studio
- c# - C# - 当我尝试创建构造函数时,出现 CS7036 错误
- python - 获取源代码不可见的网页内容
- javascript - 如何在javascript中更新数组的特定对象值
- excel - 查看隐藏工作表,无法设置工作表类的可见属性错误
- python - 获取多维数组中的最大值、最小值和最后一个索引值