RecyclerView或是ListView(列表)点击某个条目保持选中【非常巧妙】

RecyclerView或是ListView(列表)点击某个条目保持选中【非常巧妙】_第1张图片
背景:
本来想用选择器实现,结果各种不管用



    
    
    
    
    
    


实现思路:
在adapter中添加setThisPosition方法进行点击条目定位,并且调用 notifyDataSetChanged();自动执行一次adapter的getView方法,即进行按下状态的判断显示;执行点击监听时,执行一次adapter的setThisPosition(position)方法时把位置传进去,非常巧妙。

核心在Adapter实现
1dapter中添加setThisPosition方法

   //先声明一个int成员变量,若为0默认显示第一个
    private int thisPosition = -1;

    //再定义一个int类型的返回值方法
    public int getthisPosition() {
        return thisPosition;
    }

    //其次定义一个方法用来绑定当前参数值的方法
    //此方法是在调用此适配器的地方调用的,此适配器内不会被调用到
    public void setThisPosition(int thisPosition) {
        this.thisPosition = thisPosition;
//        执行构造器的getView方法
        notifyDataSetChanged();
    }

2 getView回调方法中进行点击与否状态判断

if (position == getthisPosition()) {
//            Log.e("wy", "onBindViewHolder: ");
//            viewHolder.itemView.setBackgroundResource(R.color.yellow_ui);
            viewHolder.itemView.setBackgroundResource(R.drawable.top_checked);
        } else {
            viewHolder.itemView.setBackgroundResource(R.drawable.top_normal);
        }

3 listview点击监听中执行mAdapter.setThisPosition(position);触发一切

 LvServiceCustomDetailAdapter lvServiceCustomDetailAdapter = new LvServiceCustomDetailAdapter(professionInfo.getData(), ServiceCustomDetailActivity.this);
                    lvServiceCustomDetail.setAdapter(lvServiceCustomDetailAdapter);
                    lvServiceCustomDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView parent, View view, int position, long id) {
                            lvServiceCustomDetailAdapter.setThisPosition(position);
        }
                    });

Adapter全代码

package com.wintec.huashang.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wintec.huashang.Constant;
import com.wintec.huashang.R;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class GalleryAdapter extends
        RecyclerView.Adapter {
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    ViewHolder viewHolder;
    private LayoutInflater mInflater;
    private List mDatas;
    Context mContext;

    //    private int selectedPosition = -5; //默认一个参数
    public GalleryAdapter(Context context, List datats) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    //先声明一个int成员变量,若为0默认显示第一个
    private int thisPosition = -1;

    //再定义一个int类型的返回值方法
    public int getthisPosition() {
        return thisPosition;
    }

    //其次定义一个方法用来绑定当前参数值的方法
    //此方法是在调用此适配器的地方调用的,此适配器内不会被调用到
    public void setThisPosition(int thisPosition) {
        this.thisPosition = thisPosition;
        notifyDataSetChanged();
    }

//    int a = -1;
//    public void setSelectFlag(int flag){
//        if (flag==a) {
//            return;
//        }
//        a = flag;
//        notifyDataSetChanged();
//    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView mImg;
        TextView mTxt;
        RelativeLayout mRecyclerRl;


    }


    public void setmDatas(List datas) {
        //        清除ReclyclerView条目选中状态
        thisPosition = -1;
        if (mDatas != null) {
            mDatas.clear();
            this.notifyDataSetChanged();
        }
        mDatas.addAll(datas);
//        selectedPosition = -5;
        this.notifyDataSetChanged();
    }

    public void clearnChecked() {
        viewHolder.mRecyclerRl.setFocusable(false);
        viewHolder.mRecyclerRl.clearFocus();
        viewHolder.itemView.setBackgroundResource(R.color.white);
//        selectedPosition = -5;
//        清除ReclyclerView条目选中状态
        thisPosition = -1;
        this.notifyDataSetChanged();
    }


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

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.activity_index_gallery_item,
                viewGroup, false);
//        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder = new ViewHolder(view);

//        if (!listItem.isSelect()){
//            viewHolder.relativeLayout.setBackgroundColor(Color.WHITE);
//        }else {
//            viewHolder.relativeLayout.setBackgroundColor(Color.parseColor("#ff00ff"));
//        }


//        viewHolder.mImg = (ImageView) view
//                .findViewById(R.id.id_index_gallery_item_image);
        viewHolder.mTxt = (TextView) view
                .findViewById(R.id.id_index_gallery_item_text);
        viewHolder.mRecyclerRl = (RelativeLayout) view.findViewById(R.id.recycler_rl);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

//        Log.e("wy", "onBindViewHolder: " + position+"getthisPosition(): "+getthisPosition());
        if (position == getthisPosition()) {
//            Log.e("wy", "onBindViewHolder: ");
//            viewHolder.itemView.setBackgroundResource(R.color.yellow_ui);
            viewHolder.itemView.setBackgroundResource(R.drawable.top_checked);
        } else {
            viewHolder.itemView.setBackgroundResource(R.drawable.top_normal);
        }

//        viewHolder.mImg.setImageResource(mDatas.get(position));

//        viewHolder.itemView.setSelected(selectedPosition == position);
//        if (selectedPosition == position) {
//            viewHolder.mTxt.setVisibility(View.VISIBLE);
//        } else {
//            viewHolder.mTxt.setVisibility(View.INVISIBLE);
//        }

        viewHolder.mTxt.setText(mDatas.get(position) + "");
//        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(final View v) {
//                Toast.makeText(mContext, "点击的位置:"+position +"点击的内容: "+mDatas.get(position), Toast.LENGTH_SHORT).show();
//
//            }
//        });
        //如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null) {
//按下选中
//            viewHolder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//                @Override
//                public void onFocusChange(View v, boolean hasFocus) {
//                    // TODO: 20/4/26
//                    mOnItemClickLitener.onItemClick(viewHolder.itemView, position);
//                }
//            });

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, position);
                    Log.e("wy", "run: " + "点击了ReclycerView条目");
                    Constant.isUpLoadErrorListReclyclerView = true;
//                    setSelectFlag(position);
//                    ListItem listItem = this.getData().get(position);

                }
            });


//            if(a != -1&& position == a){
                viewHolder.itemView.setSelected(true);
//
//                viewHolder.itemView.setBackgroundResource(R.color.yellow_ui);
//            }


        }


    }

}


调用

//得到控件
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        mAdapter = new GalleryAdapter(this, mDatas);
        mAdapter.setOnItemClickLitener(new GalleryAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                mAdapter.setThisPosition(position);


            }
        });


        mRecyclerView.setAdapter(mAdapter);

你可能感兴趣的:(列表点击选中条目)