1

I'm unable to play with Radio Button which is in Recyclerview. Please, check below images to get the problem.

1. First Image

First One

2. Second Image

Second One

3. Third Image

Third One

4. Fourth Image

Fourth One

What is the problem?

First Image show when this screen initially comes first time. When I select anyone option it will be highlighted in green colour and appropriate radio button get checked that is shown in Second Image.

When I want to select another option, it unchecked the previous selection but not checking the new one i.e shown in Third Image.

When I again click, It get checked which is wrong. It should unchecked previous selection and checked new selection on first click only. i.e shown in Fourth Image.

My Adapter class is like below.

    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>
Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48

2 Answers2

0

In your adapter first add local RadioButton:

private RadioButton lastCheckedRadio;

and change your onBindViewHolder like this and it should work

    @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;

        }
    });
}
tompadre
  • 797
  • 7
  • 22
0

I changed my Adapter class like below. Working fine. You can check subrahmanyam boyapati's answer on This Link.

    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());
                }
            });
        }
    }
}
Community
  • 1
  • 1
Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48