[译]RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition
来源:互联网 发布:手机录制gif软件 编辑:程序博客网 时间:2024/06/05 03:42
【未完成】
When adapter contents change (and you call notify***) RecyclerView requests a new layout. From that moment, until layout system decides to calculate a new layout (<16 ms), the layout position and adapter position may not match because layout has not reflected adapter changes yet.
当adapter中的数据发生改变(当你调用notify***),RecyclerView会重新进行layout,此时,layout position和adpater position是不一样的。
Be careful though, if you are calling notifyDataSetChanged, because it invalidates everything, RecyclerView does not know that ViewHolder’s adapter position until next layout is calculated. In that case, getAdapterPosition will return RecyclerView#NO_POSITION (-1).
But lets say if you’ve called notifyItemInserted(0), the getAdapterPosition of ViewHolder which was previously at position 0 will start returning 1 immediately. So as long as you are dispatching granular notify events, you are always in good state (we know adapter position even though new layout is not calculated yet).
Another example, if you are doing something on user click, if getAdapterPosition returns NO_POSITION, it is best to ignore that click because you don’t know what user clicked (unless you have some other mechanism, e.g. stable ids to lookup the item).
Edit For When Layout Position is Good
Lets say you are using LinearLayoutManager and want to access the ViewHolder above the currently clicked item. In that case, you should use layout position to get the item above.
mRecyclerView.findViewHolderForLayoutPosition(myViewHolder.getLayoutPosition() - 1)
You have to use layout position because it matches what user is currently seeing on the screen.
- [译]RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition
- 关于ViewHolder的getLayoutPosition和getAdapterPosition
- Recyclerview 点击事件区别 getPosition() ,getLayoutPosition(),getAdapterPosition()
- Recycle 点击事件区别 getPosition() ,getLayoutPosition(),getAdapterPosition()
- Recycle 点击事件区别 getPosition() ,getLayoutPosition(),getAdapterPosition()
- Recyclerview.getLayoutPosition()问题
- Recyclerview.getLayoutPosition()问题
- 封装RecyclerView.ViewHolder
- RecyclerView,Adapter和ViewHolder
- RecyclerView+RecyclerView.Adapter+RecyclerView.ViewHolder的使用
- ViewHolder vs HolderView
- ViewHolder VS HolderView ?
- HolderView vs ViewHolder实例
- RecyclerView通用Adapter与ViewHolder
- RecyclerView ViewHolder getAdapterPotition return NO_POSITION!
- Android viewHolder缓存RecyclerView,ListView
- 探究RecyclerView的ViewHolder复用
- RecyclerView使用Viewholder数组越界。
- UIScrollView
- HDU 4451 简单搭配问题
- 项目结构图
- DateFormat类和SimpleDateFormat类
- jqxGrid 之 Default Functionality
- [译]RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition
- Java性能调优笔记
- for循环里面用break是跳出全部循环,return是跳出本次循环
- Java关键字介绍之this与super
- 字符串和编码
- centos 7 解压rar
- 深度学习(二十九)Batch Normalization 学习笔记
- JDBC连接
- Hive(六):HQL DDL