I'm unable to play with Radio Button which is in Recyclerview. Please, check below images to get the problem.
1. First Image
2. Second Image
3. Third Image
4. Fourth Image
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>