在RecyclerView中只选择一个单选按钮

时间:2017-01-03 10:20:52

标签: android radio-button android-recyclerview

我是android开发的新手。如何从RecyclerView中仅选择一个单选按钮。 我有一个RecyclerView和cartview布局,它是RecyclerView的单个项目。我在购物车布局中有一个单选按钮。我想在recylerview中只选择单个单选按钮。我怎样才能实现这个目标。

这是我的适配器代码

 public class CardviewAdapterDateTimeLocation extends RecyclerView.Adapter<CardviewAdapterDateTimeLocation.ViewHolder>  {

 private Context mContext;
 public int lastCheckedPosition = 2;

 private static ArrayList<AdressAcivity_cardview_getter_setter> dataSet;
 public CardviewAdapterDateTimeLocation(ArrayList<AdressAcivity_cardview_getter_setter> os_versions) {

        dataSet = os_versions;
    }

    @Override
    public CardviewAdapterDateTimeLocation.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        final View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_datetimelocation_activity, null);
        CardviewAdapterDateTimeLocation.ViewHolder viewHolder = new CardviewAdapterDateTimeLocation.ViewHolder(itemLayoutView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final CardviewAdapterDateTimeLocation.ViewHolder viewHolder, final int i) {

        AdressAcivity_cardview_getter_setter fp = dataSet.get(i);
        viewHolder.addressline1.setText(fp.getAddressline1());
        viewHolder.addressline2.setText(fp.getAddressline2());
        viewHolder.addressline3.setText(fp.getAddressline3());

        viewHolder.mRemoveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String itemLabel = dataSet.get(i).toString();
                dataSet.remove(i);
                notifyItemRemoved(i);
                notifyItemRangeChanged(i,dataSet.size());
            }
        });
        viewHolder.editbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }

        });
        viewHolder.selectaddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            }
        });
        viewHolder.feed = fp;
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView addressline1;
        public TextView addressline2;
        public TextView addressline3;
        public ImageView mRemoveButton,editbtn;
        public static RadioButton selectaddress;
        public AdressAcivity_cardview_getter_setter feed;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            addressline1=   (TextView)itemLayoutView.findViewById(R.id.datetime_gridview_addressline1) ;
            addressline2 =(TextView)itemLayoutView.findViewById(R.id.datetime_gridview_addressline2);
            addressline3= (TextView) itemLayoutView.findViewById(R.id.datetime_gridview_addressline3);
            mRemoveButton=(ImageView)itemLayoutView.findViewById(R.id.datetime_gridveiw_deletebtn);
            editbtn=(ImageView)itemLayoutView.findViewById(R.id.datetime_gridveiw_editbtn);
            selectaddress=(RadioButton)itemLayoutView.findViewById(R.id.selectaddress);

            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

代码是否正常工作。

private RadioButton lastCheckedRB = null;
...
@Override
public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
   holder.priceRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
            if (lastCheckedRB != null) {
                lastCheckedRB.setChecked(false);
            }
            //store the clicked radiobutton
            lastCheckedRB = checked_rb;
        }
    });

答案 1 :(得分:0)

您可以使用布尔值b尝试在该按钮的 onCheckedChangeListener 回调中设置单选按钮的检查状态。