复选框!选择一个项目时还在RecyclerView中选择了另一个项目?

时间:2017-02-17 06:03:21

标签: android android-recyclerview

我有一个带有一些文本的回收站视图和一个复选框我希望当用户选择复选框时,只选择当前项目,但是当我向下滚动列表时 另一个项目也被选中我不知道我的代码有什么问题。 我查看了StackOverflow提供的所有解决方案,但没有人为我工作。 请告诉我我的代码有什么问题?

我的适配器类是:

public class MyAdapter extends
        RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<GetAdvReqPackagesListResponse.PackageList> packageList;
    private View rootView;

    public MyAdapter (List<GetAdvReqPackagesListResponse.PackageList> packageList,
                                              Context context) {
        this.context = context;
        this.packageList = packageList;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textPackageCode, textPackageDesc, textLaborTU, textLaborAmt,
                textPartsCount, textPartsAmt, textPackageTotal;

        CheckBox chkBoxSelectPackage;

        public MyViewHolder(View rowView) {
            super(rowView);

            rootView = rowView;

            textPackageCode = (TextView) rowView.findViewById(R.id.textPackageCode);
            textPackageDesc = (TextView) rowView.findViewById(R.id.textPackageDesc);
            textLaborTU = (TextView) rowView.findViewById(R.id.textLaborTU);
            textLaborAmt = (TextView) rowView.findViewById(R.id.textLaborAmt);
            textPartsCount = (TextView) rowView.findViewById(R.id.textPartsCount);
            textPartsAmt = (TextView) rowView.findViewById(R.id.textPartsAmt);
            textPackageTotal = (TextView) rowView.findViewById(R.id.textPackageTotal);
            chkBoxSelectPackage = (CheckBox) rowView.findViewById(R.id.chkBoxSelectPackage);

        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.shopping_cart_row_item_adv_req_package_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final GetAdvReqPackagesListResponse.PackageList packageList1 = packageList.get(position);


        holder.textPackageCode.setText(packageList.get(position).code);
        holder.textPackageDesc.setText(packageList.get(position).description);

        if (packageList.get(position).laborTU.equalsIgnoreCase("0"))
            holder.textLaborTU.setText(String.valueOf("-"));
        else
            holder.textLaborTU.setText(packageList.get(position).laborTU);

        if (packageList.get(position).laborAmt.equalsIgnoreCase("0"))
            holder.textLaborAmt.setText(String.valueOf("-"));
        else
            holder.textLaborAmt.setText(CurrencySymbol.currencySymbol(Constant.appCurrencyCode) + " "
                    + packageList.get(position).laborAmt);

        if (packageList.get(position).partCount.equalsIgnoreCase("0"))
            holder.textPartsCount.setText(String.valueOf("-"));
        else
            holder.textPartsCount.setText(packageList.get(position).partCount);

        if (packageList.get(position).partAmt.equalsIgnoreCase("0"))
            holder.textPartsAmt.setText(String.valueOf("-"));
        else
            holder.textPartsAmt.setText(CurrencySymbol.currencySymbol(Constant.appCurrencyCode) + " "
                    + packageList.get(position).partAmt);


        holder.chkBoxSelectPackage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if (isChecked == true) {
                    holder.chkBoxSelectPackage.setChecked(true);

                    holder.textPackageCode.setTextAppearance(context, R.style.font_color_green_size_13sp);
                    holder.textPackageDesc.setTextAppearance(context, R.style.font_color_green_size_13sp);
                    holder.textLaborTU.setTextAppearance(context, R.style.font_color_green_size_13sp);
                    holder.textLaborAmt.setTextAppearance(context, R.style.font_color_green_size_13sp);
                    holder.textPartsCount.setTextAppearance(context, R.style.font_color_green_size_13sp);
                    holder.textPartsAmt.setTextAppearance(context, R.style.font_color_green_size_13sp);
                } else if (isChecked == false) {
                    holder.chkBoxSelectPackage.setChecked(false);

                    holder.textPackageCode.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
                    holder.textPackageDesc.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
                    holder.textLaborTU.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
                    holder.textLaborAmt.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
                    holder.textPartsCount.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
                    holder.textPartsAmt.setTextAppearance(context, R.style.font_small_color_black_size_13sp);


                }


            }
        });


    }


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

}

提前致谢。

4 个答案:

答案 0 :(得分:1)

在GetAdvReqPackagesListResponse.PackageList中添加一个参数Selection_flag并初始化为零

read_only

答案 1 :(得分:0)

将变量“isSelected”添加到PackageList的模型calss中,并为其创建getter setter。

然后更新适配器类中的onBindViewHolder函数,如下所示:

public class MyAdapter extends
        RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<GetAdvReqPackagesListResponse.PackageList> packageList;
    private View rootView;

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final GetAdvReqPackagesListResponse.PackageList packageList1 = packageList.get(position);


        holder.textPackageCode.setText(packageList.get(position).code);
        holder.textPackageDesc.setText(packageList.get(position).description);

        if (packageList.get(position).laborTU.equalsIgnoreCase("0"))
            holder.textLaborTU.setText(String.valueOf("-"));
        else
            holder.textLaborTU.setText(packageList.get(position).laborTU);

        if (packageList.get(position).laborAmt.equalsIgnoreCase("0"))
            holder.textLaborAmt.setText(String.valueOf("-"));
        else
            holder.textLaborAmt.setText(CurrencySymbol.currencySymbol(Constant.appCurrencyCode) + " "
                    + packageList.get(position).laborAmt);

        if (packageList.get(position).partCount.equalsIgnoreCase("0"))
            holder.textPartsCount.setText(String.valueOf("-"));
        else
            holder.textPartsCount.setText(packageList.get(position).partCount);

        if (packageList.get(position).partAmt.equalsIgnoreCase("0"))
            holder.textPartsAmt.setText(String.valueOf("-"));
        else
            holder.textPartsAmt.setText(CurrencySymbol.currencySymbol(Constant.appCurrencyCode) + " "
                    + packageList.get(position).partAmt);


        holder.chkBoxSelectPackage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                packageList.get(position).setSelected(isChecked)
            }
        });

        setItemSelected(holder, packageList.get(position).isSelected);

    }

    private void setItemSelected(final MyViewHolder holder, boolean isSelected){
        if(isSelected){
            holder.chkBoxSelectPackage.setChecked(true);
            holder.textPackageCode.setTextAppearance(context, R.style.font_color_green_size_13sp);
            holder.textPackageDesc.setTextAppearance(context, R.style.font_color_green_size_13sp);
            holder.textLaborTU.setTextAppearance(context, R.style.font_color_green_size_13sp);
            holder.textLaborAmt.setTextAppearance(context, R.style.font_color_green_size_13sp);
            holder.textPartsCount.setTextAppearance(context, R.style.font_color_green_size_13sp);
            holder.textPartsAmt.setTextAppearance(context, R.style.font_color_green_size_13sp);
        } else{
            holder.chkBoxSelectPackage.setChecked(false);
            holder.textPackageCode.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
            holder.textPackageDesc.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
            holder.textLaborTU.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
            holder.textLaborAmt.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
            holder.textPartsCount.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
            holder.textPartsAmt.setTextAppearance(context, R.style.font_small_color_black_size_13sp);
        }
    }

}

答案 2 :(得分:0)

你说你想只选择当前的项目。如果您只想选择一个项目,请使用 RadioButton ,而不是 Checkbox

保留对您所选RadioButton的引用,如

private RadioButton lastChecked_CB = null;
private static int selectedPosition = -1;

然后在 onBindViewHolder()

if (position == selectedPosition) {
    holder.myRadioButton.setChecked(true);
    lastChecked_CB = holder.myRadioButton;
}

holder.myRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, final boolean isChecked) {

        if (lastChecked_CB != null) {
            lastChecked_CB.setChecked(false);
        }

        holder.myRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lastChecked_CB = holder.myRadioButton;
                selectedPosition = holder.getAdapterPosition();
            }
        });
    }
});

答案 3 :(得分:0)

这会发生,因为在滚动时会重新排列位置。下面的代码将帮助您解决问题,我已经单独发布了onBindViewHolder方法代码

private SparseArray<SparseBooleanArray> sparseArray = new SparseArray<>();

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


                holder.selectEnrollment.setId(position);
                holder.selectEnrollment.setChecked(false);

            if (sparseArray.get(holder.selectEnrollment.getId()) != null) {
                boolean isChecked = sparseArray.get(holder.selectEnrollment.getId()).get(holder.selectEnrollment.getId());
                holder.selectEnrollment.setChecked(isChecked);
            }

    holder.selectEnrollment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sparseArray.clear();

            SparseBooleanArray checkedPosition = new SparseBooleanArray();
            checkedPosition.put(v.getId(), true);
            sparseArray.put(tagId, checkedPosition);
            notifySetDataChanged();
                }
            });


    }

希望这有用:)