RecyclerView adapter position 和 layout position的区别

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.

In your use case, since your data is related to your adapter contents (and I assume that data is changed at the same time with adapter changes), you should be using adapterPosition.

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’ve used layout position because it matches what user is currently seeing on the screen.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值