Android之在RecyclerView列表中实现单选

一、实现效果

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

RecyclerView列表单选

二、实现步骤

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

1、Item布局

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp">

        <TextView
            android:id="@+id/tv_tip_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="账号1"
            android:textColor="@color/color_333333"
            android:textSize="@dimen/sp_17"
            android:gravity="center_vertical"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <ImageView
            android:id="@+id/img_selected_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:src="@mipmap/selected_tip_icon"/>

    </LinearLayout>

</LinearLayout>

2、Activity布局

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

<com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:id="@+id/refresh_view_tip_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view_select_tip"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>

3、RecyclerView的适配器

activity内部自定义适配器TipListAdapter

class TipListAdapter extends RecyclerView.Adapter<MyTipViewHolder>{

        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<String> 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!

猜你喜欢

转载自blog.csdn.net/qq_46269365/article/details/134575443
今日推荐