[译]RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition
2016-08-18 11:08
1231 查看
【未完成】
When adapter contents change (and you call notify***) RecyclerView requests a new layout.
4000
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.
When adapter contents change (and you call notify***) RecyclerView requests a new layout.
4000
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 getAdapterPotition return NO_POSITION!
- Recyclerview 点击事件区别 getPosition() ,getLayoutPosition(),getAdapterPosition()
- Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}
- RecyclerView异常:Inconsistency detected. Invalid view holder adapter positionViewHolder
- RecyclerView adapter position 和 layout position的区别
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
- 基于RecyclerView的BaseAdapter、ViewHolder、OnItemClickListener联合使用
- 属于RecyclerView的万能适配器Adapter和ViewHolder
- 基类RecyclerViewAdapter以及BaseViewHolder。减少适配器的重复代码量
- RecycleView getAdapterPosition() 返回-1
- 解决RecyclerView.getChildAt(Position)崩溃
- RecyclerView+RecyclerView.Adapter+RecyclerView.ViewHolder的使用
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- Android RecyclerView(五)封装Holder与Adapter(Android 5.0 新特性)
- 安卓开发(5):自定义RecyclerView.Adapter、RecyclerView和自定义RecyclerView.ViewHolder的使用
- Error:(13, 8) 错误: xxx不是抽象的, 并且未覆盖xxx中的抽象方法onBindViewHolder(BaseSimpleRecyclerAdapter.ViewHolder,int)
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
- Recyclerview.getLayoutPosition()问题
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案