Android implements radio selection in RecyclerView list

1. Realization effect

Radio selection, can be deselected, list data can be updated (the selection status is cleared and can be reselected)

RecyclerView list radio selection

2. Implementation steps

Only part of the core code is shown, please mainly refer to the definition of the adapter

1. Item layout

selected_tip_list_item.xml file
contains a TextView and an 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 layout

For the use of SmartRefreshLayout with RecyclerView, please refer to another article:SmartRefreshLayout+RecyclerView in Android implements pull-down refresh and pull-up loading (paging) to display network request data

<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. Adapter for RecyclerView

Activity internal custom adapter 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. Example of calling within Activity

(1) Binding controls
@InjectView(id = R.id.refresh_view_tip_list)
private SmartRefreshLayout mRefreshLayout;

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

(2) Initialization list and adapter

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) After data request, update adapter

The update list calling method is as follows:
(mList is the requested data)

adapter.refreshData(mList);

3. Done, Nice!

Guess you like

Origin blog.csdn.net/qq_46269365/article/details/134575443