Android之在RecyclerView列表中实现单选

一、实现效果

单选、可取消选中、列表数据可更新(选择状态清空,可重新选择)

RecyclerView列表单选

二、实现步骤

仅展示部分核心代码,请主要参考适配器的定义

1、Item布局

selected_tip_list_item.xml文件
包含一个TextView和一个右侧选中的图片ImageView√




    

        
        
        

    


2、Activity布局

SmartRefreshLayout搭配RecyclerView的使用可参考另一篇文章:Android中SmartRefreshLayout+RecyclerView实现下拉刷新和上拉加载(分页)显示网络请求数据



            

3、RecyclerView的适配器

activity内部自定义适配器TipListAdapter

class TipListAdapter extends RecyclerView.Adapter{

        private boolean isClick = false;
        private int mPosition = -1; //当前已选中位置

        public int getmPosition() {
            return mPosition;
        }

        public boolean isClick() {
            return isClick;
        }

        public void setmPosition(int mPosition,boolean isClick) {
            this.mPosition = mPosition;
            this.isClick = isClick;
        }

        @NonNull
        @Override
        public MyTipViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = View.inflate(AddTipActivity.this,R.layout.select_tip_list_item, null);
            MyTipViewHolder holder = new MyTipViewHolder(v);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //将点击选中的Item信息赋值给上方的输入框
                    mEditTipName.setText(holder.mTvTipName.getText().toString());
                    if (!isClick){
                        setmPosition(holder.getBindingAdapterPosition(),true);
                    }else{
                        setmPosition(holder.getBindingAdapterPosition(),getmPosition() != holder.getBindingAdapterPosition());
                    }
                    notifyDataSetChanged();
                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyTipViewHolder holder, int position) {
            String info = mTipBeanList.get(position);
            holder.mTvTipName.setText(info);
            if (getmPosition() == position && isClick) {
                //选中某行
                holder.itemView.setBackground(getDrawable(R.drawable.finished_product_selected_button_background2));
                holder.mTvTipName.setTextColor(ContextCompat.getColor(AddTipActivity.this,R.color.color_D32124));
                holder.mImgSelectedIcon.setVisibility(View.VISIBLE);
            }else if (getmPosition() == position && !isClick){
                //取消已选中行
                mEditTipName.setText("");
                holder.itemView.setBackground(getDrawable(R.drawable.unselected_tip_background));
                holder.mImgSelectedIcon.setVisibility(View.GONE);
                holder.mTvTipName.setTextColor(ContextCompat.getColor(AddTipActivity.this,R.color.color_333333));
            }
            else {
                holder.itemView.setBackground(getDrawable(R.drawable.unselected_tip_background));
                holder.mImgSelectedIcon.setVisibility(View.GONE);
                holder.mTvTipName.setTextColor(ContextCompat.getColor(AddTipActivity.this,R.color.color_333333));
            }
        }

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

        //列表更新时调用的方法
        public void refreshData(List data) {
            mTipBeanList.clear();
            mTipBeanList.addAll(data);
            //列表数据刷新时所有Item均恢复未选中状态
            setmPosition(-1,false);
            notifyDataSetChanged();
        }
    }

    class MyTipViewHolder extends RecyclerView.ViewHolder{
        TextView mTvTipName;
        ImageView mImgSelectedIcon;  

        MyTipViewHolder(View itemView) {
            super(itemView);
            mTvTipName = itemView.findViewById(R.id.tv_tip_name);
            mImgSelectedIcon = itemView.findViewById(R.id.img_selected_icon);
        }
    }

3、Activity内调用示例

(1)绑定控件
@InjectView(id = R.id.refresh_view_tip_list)
private SmartRefreshLayout mRefreshLayout;

@InjectView(id = R.id.recycler_view_select_tip)
private RecyclerView mRecyclerViewAllTips;

(2)初始化列表和适配器

adapter = new TipListAdapter();
mRecyclerViewAllTips.setAdapter(adapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerViewAllTips.setLayoutManager(linearLayoutManager);
mRecyclerViewAllTips.setItemAnimator(new DefaultItemAnimator());
//设置下拉刷新和上拉加载样式
mRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
mRefreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){
        //下拉刷新
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
            //????这里去调用请求数据的方法,并更新adapter
            mRecyclerViewAllTips.smoothScrollToPosition(0);
            mRefreshLayout.finishRefresh(1000);
        }
      });
//首次进入页面自动刷新
mRefreshLayout.autoRefresh();
(3)数据请求后,更新adapter

更新列表调用方法如下:
(mList为请求到的数据)

adapter.refreshData(mList);

三、完成,Nice!

你可能感兴趣的:(Android,安卓控件,android,安卓控件)