我有一个Recycler视图,其中包含单选按钮。
现在我只想从列表中选择一个单选按钮并获取它的值
进入活动。
下面附上了屏幕截图。
下面是我到目前为止尝试过的链接。
how to select just one RadioButton with RecyclerView android?
Getting previous RadioButton value from RadioGroup inside a RecyclerView
但什么都没出来。
这是过滤器适配器的代码
public class FilterValRecyclerAdapter extends
RecyclerView.Adapter<FilterValRecyclerAdapter.ValueViewHolder> {
private final FragmentActivity context;
private final ArrayList<FilterDefaultMultipleListModel> filterModels;
private FilterDefaultMultipleListModel filterDefaultMultipleListModel;
private final int resource;
private final int type;
OnItemClickListener mItemClickListener;
private RadioButton lastCheckedRB = null;
private int rbPosoition =0;
private int mSelectedItem = -1;
public FilterValRecyclerAdapter(FragmentActivity context, int
filter_list_item_layout,ArrayList<FilterDefaultMultipleListModel>
filterModels int type) {
this.context = context;
this.filterModels = filterModels;
this.resource = filter_list_item_layout;
this.type = type;
}
@Override
public ValueViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(this.context)
.inflate(resource, viewGroup, false);
return new ValueViewHolder(v,this.type);
}
@Override
public void onBindViewHolder(final ValueViewHolder viewHolder, final int
position) {
if(type==2){
viewHolder.rdgCategoryItems.setVisibility(View.VISIBLE);
viewHolder.rdCategoryItem.setVisibility(View.VISIBLE);
viewHolder.cbSelected.setVisibility(View.GONE);
viewHolder.subCategoryName.setText(filterModels.get(position).getName());
viewHolder.rdCategoryItem.setChecked(position == mSelectedItem);
}else{
viewHolder.rdgCategoryItems.setVisibility(View.GONE);
viewHolder.rdCategoryItem.setVisibility(View.GONE);
viewHolder.cbSelected.setVisibility(View.VISIBLE);
viewHolder.subCategoryName.setText(filterModels.get(position).getName());
viewHolder.cbSelected.setChecked(filterModels.get(position).isChecked());
}
}
@Override
public int getItemCount() {
return filterModels.size();
}
public void setItemSelected(int position) {
if (position != -1) {
filterModels.get(position).setChecked
(!filterModels.get(position).isChecked());
notifyDataSetChanged();
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(final OnItemClickListener
mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public class ValueViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
TextView subCategoryName;
CheckBox cbSelected;
RadioButton rdCategoryItem;
RadioGroup rdgCategoryItems;
View colorView;
public int type;
ValueViewHolder(View itemView, int type) {
super(itemView);
itemView.setOnClickListener(this);
subCategoryName = (TextView)
itemView.findViewById(R.id.txt_item_list_title);
cbSelected = (CheckBox) itemView.findViewById(R.id.cbSelected);
rdCategoryItem = (RadioButton)
itemView.findViewById(R.id.rdCategoty);
rdgCategoryItems = (RadioGroup)
itemView.findViewById(R.id.rdgCategory);
colorView = itemView.findViewById(R.id.colored_bar);
this.type = type;
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
}
}
任何帮助将不胜感激。
先谢谢了。
答案 0 :(得分:0)
在onCheckedChnageListener()
方法中添加onBindViewHolder()
,并在mSelectedItem
内部维护onCheckedChnageListener()
viewHolder.rdCategoryItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSelectedItem=position;
notifyDataSetChanged();
}
});