回收者视图中的单选单选按钮(重复)

时间:2019-01-08 11:41:33

标签: android android-recyclerview radio-button

我有一个Recycler视图,其中包含单选按钮。

现在我只想从列表中选择一个单选按钮并获取它的值

进入活动。

下面附上了屏幕截图。

Screenshots of Recycler view

下面是我到目前为止尝试过的链接。

  1. http://joshskeen.com/building-a-radiogroup-recyclerview/

  2. Single selection in RecyclerView

  3. http://www.zoftino.com/android-recyclerview-radiobutton

  4. Select only one radiobutton in a recyclerview

  5. how to select just one RadioButton with RecyclerView android?

  6. Getting previous RadioButton value from RadioGroup inside a RecyclerView

  7. Using Radio button with recyclerview in android

但什么都没出来。

这是过滤器适配器的代码

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());


        }
      }
   }
  }

任何帮助将不胜感激。

先谢谢了。

1 个答案:

答案 0 :(得分:0)

onCheckedChnageListener()方法中添加onBindViewHolder(),并在mSelectedItem内部维护onCheckedChnageListener()

 viewHolder.rdCategoryItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             mSelectedItem=position;
             notifyDataSetChanged();
            }
        });