单一单选按钮检查取消选中recyclerview中的问题

时间:2017-05-17 13:48:37

标签: android-recyclerview recycler-adapter android-radiogroup android-radiobutton

我无法使用Recyclerview中的Radio Button。请检查下面的图像以解决问题。

1。第一张图片

First One

2。第二张图片

Second One

第3。第三张图片

Third One

4。第四张图片

Fourth One

有什么问题?

第一张图片显示此屏幕最初是第一次出现的时间。当我选择任何选项时,它将以绿色突出显示,并选中相应的单选按钮,显示在第二张图像中。

如果我想选择其他选项,则取消选中上一个选项,但不检查新选项,即第三张图片中显示。

当我再次点击时,会检查哪个是错误的。它应该取消选中先前的选择并仅在第一次单击时检查新选择。即显示在第四张图片

我的适配器类如下所示。

    public class PaymentOptionsAdapter extends RecyclerView.Adapter<PaymentOptionsAdapter.ViewHolder> {

    Context context;
    List<String> listOfPaymentOptions;
    int mCheckedPostion = -1;
    private boolean isChecked = false;

    public PaymentOptionsAdapter(Context context, List<String> listOfPaymentOptions) {
        this.context = context;
        this.listOfPaymentOptions = listOfPaymentOptions;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_payment_options, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.txtOption.setText(listOfPaymentOptions.get(position));
        if (mCheckedPostion == position) {
            holder.txtOption.setTextColor(context.getResources().getColor(R.color.green_txt));
            holder.radioSelect.setChecked(true);
            if (position == 0) {
                new AppShare(context).setPaymentOption(context.getString(R.string.cod));
            } else if (position == 1) {
                new AppShare(context).setPaymentOption(context.getString(R.string.payu));
            } else if (position == 2) {
                new AppShare(context).setPaymentOption(context.getString(R.string.online));
            }
        } else {
            holder.txtOption.setTextColor(context.getResources().getColor(R.color.black));
            holder.radioSelect.setChecked(false);
        }

        holder.layoutContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isChecked) {
                    notifyItemChanged(mCheckedPostion);
                    mCheckedPostion = position;
                    notifyItemChanged(mCheckedPostion);
                    isChecked = true;
                } else {
                    notifyItemChanged(mCheckedPostion);
                    mCheckedPostion = -1;
                    notifyItemChanged(position);
                    isChecked = false;
                    new AppShare(context).setPaymentOption(null);
                }
            }
        });

        holder.radioSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isChecked) {
                    notifyItemChanged(mCheckedPostion);
                    mCheckedPostion = position;
                    notifyItemChanged(mCheckedPostion);
                    isChecked = true;
                } else {
                    notifyItemChanged(mCheckedPostion);
                    mCheckedPostion = -1;
                    notifyItemChanged(position);
                    isChecked = false;
                    new AppShare(context).setPaymentOption(null);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout layoutContainer;
        TextView txtOption;
        RadioButton radioSelect;

        public ViewHolder(View itemView) {
            super(itemView);
            layoutContainer = (RelativeLayout) itemView.findViewById(R.id.layoutContainer);
            txtOption = (TextView) itemView.findViewById(R.id.txtOption);
            radioSelect = (RadioButton) itemView.findViewById(R.id.radioSelect);
            txtOption.setTypeface(FontUtils.getInstance(context).getRobotoTypeFace());
        }
    }
}

row_payment_options.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/card_view_height"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/layoutContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="@dimen/large_margin"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="@dimen/text_title" />

        <RadioButton
            android:id="@+id/radioSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:checked="false"
            android:paddingRight="@dimen/large_margin" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="@dimen/large_margin"
            android:layout_marginRight="@dimen/x_large_margin"
            android:layout_toLeftOf="@+id/radioSelect"
            android:background="@color/gray_border" />
    </RelativeLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在适配器中首先添加本地RadioButton:

private RadioButton lastCheckedRadio;

并像这样更改你的onBindViewHolder,它应该可以正常工作

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

    holder.txtOption.setText(listOfPaymentOptions.get(position));
    holder.radioSelect.setTag(new Integer(position));

    if(position == 0 && holder.radioSelect.isChecked())
    {
        lastCheckedRadio = holder.radioSelect;
        mCheckedPostion = 0;
    }


    holder.layoutContainer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v)
        {

            int clickedPos = ((Integer)holder.radioSelect.getTag()).intValue();

            if(mCheckedPostion!=position)
            {
                if(lastCheckedRadio != null)
                {
                    lastCheckedRadio.setChecked(false);
                }

                lastCheckedRadio = holder.radioSelect;
                lastCheckedRadio.setChecked(true);
                mCheckedPostion = clickedPos;
                if (position == 0) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.cod));
                } else if (position == 1) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.payu));
                } else if (position == 2) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.online));
                }

            }
            else
                lastCheckedRadio = holder.radioSelect;

        }
    });

    holder.radioSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int clickedPos = ((Integer)holder.radioSelect.getTag()).intValue();

            if(holder.radioSelect.isChecked())
            {
                if(lastCheckedRadio != null)
                {
                    lastCheckedRadio.setChecked(false);
                    holder.layoutContainer.setFocusable(false);
                }

                lastCheckedRadio = holder.radioSelect;
                lastCheckedRadio.setChecked(true);
                mCheckedPostion = clickedPos;
                if (position == 0) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.cod));
                } else if (position == 1) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.payu));
                } else if (position == 2) {
                    new AppShare(context).setPaymentOption(context.getString(R.string.online));
                }
            }
            else
                lastCheckedRadio = holder.radioSelect;

        }
    });
}

答案 1 :(得分:0)

我改变了我的Adapter类,如下所示。工作正常。您可以在This Link上查看 subrahmanyam boyapati的答案。

    public class PaymentOptionsAdapter extends RecyclerView.Adapter<PaymentOptionsAdapter.ViewHolder> {

    Context context;
    List<String> listOfPaymentOptions;
    private int lastCheckedPosition = -1;

    public PaymentOptionsAdapter(Context context, List<String> listOfPaymentOptions) {
        this.context = context;
        this.listOfPaymentOptions = listOfPaymentOptions;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_payment_options, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.txtOption.setText(listOfPaymentOptions.get(position));
        holder.radioSelect.setChecked(position == lastCheckedPosition);

        if (lastCheckedPosition == position) {
            holder.txtOption.setTextColor(context.getResources().getColor(R.color.green_txt));
            holder.radioSelect.setChecked(true);
            if (position == 0) {
                new AppShare(context).setPaymentOption(context.getString(R.string.cod));
            } else if (position == 1) {
                new AppShare(context).setPaymentOption(context.getString(R.string.payu));
            } else if (position == 2) {
                new AppShare(context).setPaymentOption(context.getString(R.string.online));
            } else {
                new AppShare(context).setPaymentOption(null);
            }
        } else {
            holder.txtOption.setTextColor(context.getResources().getColor(R.color.black));
            holder.radioSelect.setChecked(false);
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout layoutContainer;
        TextView txtOption;
        RadioButton radioSelect;

        public ViewHolder(View itemView) {
            super(itemView);
            layoutContainer = (RelativeLayout) itemView.findViewById(R.id.layoutContainer);
            txtOption = (TextView) itemView.findViewById(R.id.txtOption);
            radioSelect = (RadioButton) itemView.findViewById(R.id.radioSelect);
            txtOption.setTypeface(FontUtils.getInstance(context).getRobotoTypeFace());

            layoutContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lastCheckedPosition = getAdapterPosition();
                    notifyItemRangeChanged(0, listOfPaymentOptions.size());
                }
            });

            radioSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lastCheckedPosition = getAdapterPosition();
                    notifyItemRangeChanged(0, listOfPaymentOptions.size());
                }
            });
        }
    }
}