美文网首页
recycleview 做一个radiobutton的列表

recycleview 做一个radiobutton的列表

作者: StoneWay3 | 来源:发表于2018-06-12 17:30 被阅读0次

    项目中有需求,参考网上其他的文章写了一篇,有很多不足之处,可以参考,如果有更好的想法,欢迎指点。好了开始吧

    1.recycleview的布局

    <android.support..v7.widget.RecyclerView

    android:id="@+id/wuliu_recyclerview"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"/>

    2.数据

    public class WuLiuBean{

    public WuLiuBean(String name,boolean isSelected) { this.name = name; setSelected(isSelected); }

    private String name;

    private boolean isSelected;

    public StringgetName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public boolean isSelected() {

    return isSelected;

    }

    public void setSelected(boolean selected) {

    isSelected = selected;

    }

    }

    3.activity中添加数据

    List datas =new ArrayList<>();

    List datasadd =new ArrayList<>();

    for (int i =0; i <1; i++) {

    datas.add(new WuLiuBean("快"));

        datas.add(new WuLiuBean("你呢", i ==0 ?true :false));

        datas.add(new WuLiuBean("方通"));

        datas.add(new WuLiuBean("各种痛"));

        datas.add(new WuLiuBean("天天"));

        datas.add(new WuLiuBean("丽丽"));

    }

    datasadd .addAll(datas );

    4.适配器布局

    <LinearLayout

      xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

        android:layout_height="wrap_content" android:orientation="horizontal">

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/wuliu_rb_item"

        android:textDirection="rtl"

        android:gravity="center_vertical"

        android:padding="10dp"/>

    5适配器

    import android.content.Context;

    import android.content.Intent;

    import android.os.Bundle;

    import android.support.v7.widget.RecyclerView;

    import android.util.Log;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.CompoundButton;

    import android.widget.ImageView;

    import android.widget.RadioButton;

    import android.widget.TextView;

    import java.util.List;

    import cn.idcby.hwdemo.R;

    import cn.idcby.hwdemo.activity.goods.SureGoodsActivity;

    import cn.idcby.hwdemo.beans.WuLiuBean;

    public class WuliuAdapterextends RecyclerView.Adapter {

    private ListmDatas;

        private ContextmContext;

        private LayoutInflatermInflater;

        private int mSelectedPos = -1;//实现单选  方法二,变量保存当前选中的position

        private RecyclerViewmRv;//实现单选方法三: RecyclerView另一种定向刷新方法:

        public WuliuAdapter(List datas, Context context, RecyclerView rv) {

    mDatas = datas;

            mContext = context;

            mInflater = LayoutInflater.from(mContext);

            mRv = rv;

            // 设置数据集时,找到默认选中的pos

            for (int i =0; i

    if (mDatas.get(i).isSelected()) {

    mSelectedPos = i;

                }

    }

    }

    @Override

        public CouponVHonCreateViewHolder(ViewGroup parent, int viewType) {

    return new CouponVH(mInflater.inflate(R.layout.item_wuliu_adapter, parent, false));

        }

    @Override

        public void onBindViewHolder(final CouponVH holder, final int position) {

            holder.mRB.setChecked(mDatas.get(position).isSelected());

            holder.mRB.setText(mDatas.get(position).getName());

            holder.mRB.setOnClickListener(new View.OnClickListener() {

    @Override

                public void onClick(View view) {

                    CouponVH couponVH = (CouponVH)mRv.findViewHolderForLayoutPosition(mSelectedPos);

                    if (couponVH !=null) {

                        couponVH.mRB.setChecked(false);

                    }else {

                        notifyItemChanged(mSelectedPos);

                    }

    mDatas.get(mSelectedPos).setSelected(false);

                    mSelectedPos =position;

                    mDatas.get(mSelectedPos).setSelected(true);

                    holder.mRB.setChecked(true);

                    if (subClickListener !=null) {

    subClickListener.OntopicClickListener(mDatas.get(position), position);

                    }

                }

    });

            if (mDatas.size()>0){

    if (holder.mRB.isChecked()){

    if (subClickListener !=null) {

    subClickListener.OntopicClickListener(mDatas.get(position), position);

                    }

    }

    }

    }

    @Override

        public void onBindViewHolder(final CouponVH holder, int position, List payloads) {

            if (payloads.isEmpty()) {

    onBindViewHolder(holder, position);

            }else {

    Bundle payload = (Bundle) payloads.get(0);

                if (payload.containsKey("KEY_BOOLEAN")) {

    final boolean aBoolean = payload.getBoolean("KEY_BOOLEAN");

                    holder.mRB.setChecked(aBoolean);

                }

    }

    }

    @Override

        public int getItemCount() {

    return null !=mDatas ?mDatas.size() :0;

        }

    public static class CouponVHextends RecyclerView.ViewHolder {

    private RadioButtonmRB;

            public CouponVH(View itemView) {

    super(itemView);

                mRB=(RadioButton)itemView.findViewById(R.id.wuliu_rb_item);

            }

    }

    ////////////////////////////

        private SubClickListenersubClickListener;

        public void setsubClickListener(SubClickListener topicClickListener) {

    this.subClickListener = topicClickListener;

        }

    public interface SubClickListener {

    void OntopicClickListener( WuLiuBean detail, int position);

        }

    }

    6activity中

    mRecyclerview.setLayoutManager(new LinearLayoutManager(this));

    WuliuAdapter wuliuAdapter=new WuliuAdapter(datasadd , this, mRecyclerview);

    mRecyclerview.setAdapter(wuliuAdapter);

    wuliuAdapter.setsubClickListener(new WuliuAdapter.SubClickListener() {

    @Override

        public void OntopicClickListener( WuLiuBean detail, int position) {

    if (detail!=null){

    //拿到返回的数据做进一步的处理

    ComToastUtils.showToast(mContext,detail.getName());

            }

    }

    });

    相关文章

      网友评论

          本文标题:recycleview 做一个radiobutton的列表

          本文链接:https://www.haomeiwen.com/subject/ttdreftx.html