RecyclerView实现条目单选、条目多选
效果
apk演示下载: http://7xstkb.com1.z0.glb.clouddn.com/RecyclerViewSelector.apk
依赖
implementation 'com.android.support:recyclerview-v7:26.1.0'
1.点击选中效果(单选/多选)
* Created by agen on 2018/3/9. * 两行代码轻松添加点击选中效果(单选/多选) *1.记录按压点击的位置 是否等于 当前点击的位置 * 2.1刷新当前点击item条目、下一个条目、上一个条目(单选) * 2.2刷新当前点击item条目(多选)
设置适配器Adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public int selectedPosition = -5; //默认一个参数
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { MyViewHolder myViewHolder = (MyViewHolder) holder; String index = (String) mDatas.get(position); myViewHolder.textView.setText("index=" + index); //记录按压点击的位置 是否等于 当前点击的位置 ? if (selectedPosition == position) { myViewHolder.imageView.setVisibility(View.VISIBLE); } else { myViewHolder.imageView.setVisibility(View.INVISIBLE); } /**设置条目的点击事件*/ myViewHolder.itemView.setTag(myViewHolder); myViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onItemClickListener.OnItemClick(view, (MyViewHolder) holder,holder.getAdapterPosition()); } }); }
设置点击效果-单选
private void initData() { list = new ArrayList<>(); for (int i = 0; i < 8; i++) { list.add(i, "" + i); } myAdapter = new MyAdapter(this, list); myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void OnItemClick(View view, MyAdapter.MyViewHolder holder, int position) { Toast.makeText(view.getContext(), "position=" + position, Toast.LENGTH_SHORT).show(); /**刷新当前点击item条目、下一个条目、上一个条目*/ myAdapter.setSelectedPosition(position);//选择的position赋值给参数, int selectedPosition = myAdapter.getSelectedPosition(); Log.d(TAG, "selectedPosition=" + selectedPosition); myAdapter.notifyItemChanged(selectedPosition);//刷新当前点击item myAdapter.notifyItemChanged(selectedPosition + 1);//刷新当前点击item的下一个条目 if (selectedPosition >= 1) { myAdapter.notifyItemChanged(selectedPosition - 1);//刷新当前点击item上一个条目 } } }); recycler_view.setAdapter(myAdapter); }
设置点击效果-多选
if (null != myAdapter) { //两行代码轻松添加点击选中效果,实现条目的多选 /**刷新当前点击item条目*/ myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void OnItemClick(View view, MyAdapter.MyViewHolder holder, int position) { Toast.makeText(view.getContext(), "position=" + position, Toast.LENGTH_SHORT).show(); myAdapter.setSelectedPosition(position);//选择的position赋值给参数, int selectedPosition = myAdapter.getSelectedPosition(); Log.d(TAG, "selectedPosition=" + selectedPosition); myAdapter.notifyItemChanged(selectedPosition);//刷新当前点击item } }); }
为RecyclerView添加点击事件与效果
/** * Created by Administrator on 2017/7/6 0006. * 为RecyclerView添加点击事件与效果 * (两行代码轻松添加点击选中效果) * 1在主题中获取RecyclerView控件的属性 * 2使用按压图片selector选择器 */ public class MyAdapterTwo extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false); TypedValue typedValue = new TypedValue(); parent.getContext().getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true); //系统自带的条目选择效果 // view.setBackgroundResource(typedValue.resourceId); /**自定义条目按压效果state_pressed*/ view.setBackgroundResource(R.drawable.selector_shape_blue_gray); MyViewHolder myViewHolder = new MyViewHolder(view, mOnItemClickListener); return myViewHolder; }
。。。