RecyclerView添加选中效果

更新时间:2023-06-23 10:59:29 阅读: 评论:0

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,实际上绘制红⾊区域即可。

本文发布于:2023-06-23 10:59:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1020601.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:选中   添加   视频
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图