单选按钮实现

1、创建一个Been ,记录状态

public class DataBeen {
    public boolean isChecked = false;

}

2.适配器中操作点击事件,用来显示和隐藏

public class RecyclerViewAdapter extends RecyclerView.Adapter{


    public RecyclerViewAdapter(List data) {
        mData = data;
    }

    private List mData;
    private int selecterPostion = -1;
    public boolean isBind;
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerholderitem, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {


        isBind = true;
        final DataBeen mainBean = mData.get(position);
        if (mainBean.isChecked){
            holder.mImage2.setVisibility(View.VISIBLE);
        }else{
            holder.mImage2.setVisibility(View.INVISIBLE);
        }

        holder.mImage1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selecterPostion != position && selecterPostion != -1) {
                    mData.get(selecterPostion).isChecked = false;
                    notifyItemChanged(selecterPostion);
                }
                selecterPostion = position;
                mData.get(position).isChecked = true;
                notifyItemChanged(position);
            }
        });



    }

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

    public  interface OnClickListener{
        void onClick(ViewHolder holder,int position);

    }

    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final View mImage1;

        public View getImage2() {
            return mImage2;
        }

        private final View mImage2;

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

            mImage1 = itemView.findViewById(R.id.image1);
            mImage2 = itemView.findViewById(R.id.image2);
        }
    }
}

3.适配器布局



    

    

4.MainActivity中的添加数据

public class MainActivity extends AppCompatActivity {

    private RecyclerView recy;
    private String sss = "asdasdasdasdasd";
    private List datas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recy = (RecyclerView) findViewById(R.id.recyclerview);
        datas = new ArrayList<>();
        recy.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        for (int i = 0; i < sss.length(); i++) {
            DataBeen dataBeen = new DataBeen();
            datas.add(dataBeen);
        }
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(datas);
        recy.setAdapter(recyclerViewAdapter);

    }
}

 

你可能感兴趣的:(移动开发)