-
-
Save joshskeen/4fcd3f7ba7d37ee69fee to your computer and use it in GitHub Desktop.
abstract class RadioAdapter<T> extends RecyclerView.Adapter<RadioAdapter.ViewHolder> { | |
public int mSelectedItem = -1; | |
private Context mContext; | |
private List<T> mItems; | |
public RadioAdapter(Context context, List<T> items) { | |
mContext = context; | |
mItems = items; | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder viewHolder, final int i) { | |
viewHolder.mRadio.setChecked(i == mSelectedItem); | |
} | |
@Override | |
public int getItemCount() { | |
return mItems.size(); | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { | |
final View view = LayoutInflater.from(mContext).inflate(R.layout.view_radio_item, viewGroup, false); | |
return new ViewHolder(view); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
public RadioButton mRadio; | |
public TextView mText; | |
public ViewHolder(final View inflate) { | |
super(inflate); | |
mText = (TextView) inflate.findViewById(R.id.text); | |
mRadio = (RadioButton) inflate.findViewById(R.id.radio); | |
View.OnClickListener l = v -> { | |
mSelectedItem = getAdapterPosition(); | |
notifyItemRangeChanged(0, mItems.size()); | |
}; | |
itemView.setOnClickListener(l); | |
mRadio.setOnClickListener(l); | |
} | |
} | |
} |
Having a RadioButton without a RadioGroup in a RecyclerView. The TextView is just required if you want to have some special appearance, RadioButton has a setText method.
What is this itemView.setOnClickListener(l);
Thank you very much. well itemView
is inflate
in your context, Hope you change it.
Android Studio doesn't support Java 8, so how did you get your lambda expression to compile (starting at line 37)?
@scottbiggs I would assume via Retrolambda
How then you are going to get the selected item in the class using this adapter class?
@mutexkid thanks
Thank u
Thank you so much, it helped me not to do it stupidly.
thanks, this clean my issue
I used the tutorial above. I have 3 radio buttons. when I checked on the 4x radio button it couldn't be checked but in the log it managed to bring up the index and text of the radio button
please describe the purpose of this class :)