RecyclerView添加选中效果
在线闹铃RecyclerView添加选中效果
两种⽅案
通过lector
该⽅案实现简单,适合背景图案是固定的item,需要注意⼀点:在adapter点击事件中调⽤notifyItemChanged(lectedPosition)来刷新界⾯
@Override
public void onBindViewHolder(StateHolder holder, int position){
if(lectedPosition == position){
holder.ivSelected.tVisibility(View.VISIBLE);
}el{
holder.ivSelected.tVisibility(View.INVISIBLE);
}
holder.itemView.tOnClickListener(view ->{
onItemClickListener.OnItemClick(view, holder, AdapterPosition());
lectedPosition = position;
notifyItemChanged(lectedPosition);//刷新当前点击item
});
}女式手表
通过ItemDecoration
该⽅案实现复杂些,⽀持⾃定义view,需要重写getItemOffts和onDraw
例如视频列表界⾯,选中后在视频的预览图上⾯有⼀条红⾊线,代表被选中,这样⽤户在播放界⾯回到列表界⾯,知道刚才播放的是哪个视频,效果图如下:
下⾯介绍该⽅案的实现。
修改activity
在activity中,recyclerView添加ItemDecoration,adapter添加itemDecoration监听器
探究式学习VideoItemDecoration继承ItemDecoration,实现OnItemClickListener接⼝,下⾯会具体介绍
itemDecoration =new VideoItemDecoration(videoListAdapter);猫的特点作文
recyclerView.addItemDecoration(itemDecoration);
videoListAdapter.tOnItemClickListener(itemDecoration);
修改adapter
在adapter中增加⼀个接⼝,将position回调给ItemDecoration
public void tOnItemClickListener(OnItemClickListener onItemClickListener){
}
public interface OnItemClickListener {
/**
* onItemClick
*
带春的词语
* @param view 当前的view
* @param position 当前的position
*/
void onItemClick(View view, int position);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i){
myViewHolder.itemView.tTag(i);
myViewHolder.itemView.tOnClickListener(v ->{
环境应急预案if(null!= onItemClickListener){
}
});
怎样做海参Glide.with(mContext).load(Uri.fromFile(new (i).getPath())))
.into(myViewHolder.imageView);
myViewHolder.nameTitleView.(i).getTitle());
}
创建VideoItemDecoration
VideoItemDecoration继承ItemDecoration,实现OnItemClickListener监接⼝,重写getItemOffts和onDraw
public class VideoItemDecoration extends RecyclerView.ItemDecoration implements VideoListAdapter.OnItemClickListener {
private int lectedPosition =-1;
private Paint mPaint;
private static final int DIVIDER=1;//线宽
private static final int DIVIDER_COLOR= Color.RED;
private RecyclerView.Adapter mAdapter;
public VideoItemDecoration(RecyclerView.Adapter adapter){
this.mAdapter = adapter;
mPaint =new Paint();
mPaint.tAntiAlias(true);
mPaint.tColor(DIVIDER_COLOR);
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state){
int childCount = ChildCount();
for(int i =0; i < childCount; i++){
View view = ChildAt(i);
int index = ChildAdapterPosition(view);
if(index == lectedPosition){
float dividerTop = Top()-DIVIDER;
float dividerLeft = Left();
float dividerBottom = Top();
float dividerRight = dividerLeft + Width();
c.drawRect(dividerLeft, dividerTop, dividerRight, dividerBottom, mPaint);
}
}
}
读书人的名言名句@Override
public void getItemOffts(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state){ ItemOffts(outRect, view, parent, state);
ChildAdapterPosition(view)== lectedPosition){
}el{
}
}
@Override
public void onItemClick(View view, int position){
lectedPosition = position;
}
}
关于onDraw的绘制,我画了⼀个⽰意图,如下:
1. 画红⾊线实际上是绘制outRec;
2. outRect只指定了top,实际上绘制红⾊区域即可。