search

Home  >  Q&A  >  body text

android - getLayoutPosition与getAdapterPosition

看了stackoverflow RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition上的解释

然后又看了 它俩的源码的注释

还是没有分清它俩的具体区别

有没有人具体的说明一下??!

高洛峰高洛峰2772 days ago676

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:47:11

    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.

    The answer is very clear. The specific difference is that there will be a time difference between the positions of the adapter and layout (<16ms). If you change the data of the Adapter and then refresh the view, it will take a while for the layout to update the view. In this paragraph In time, the positions returned by these two methods will be different.

    In addition, the answer also mentioned that the position in the Adapter cannot be obtained immediately after notifyDataSetChanged, and can only be obtained after the layout is completed. notifyDataSetChanged之后并不能马上获取Adapter中的position, 要等布局结束之后才能获取到.

    而对于Layout的position, 在notifyItemInserted

    As for Layout's position, after notifyItemInserted, Layout cannot get the new position immediately because the layout has not been updated yet (it takes <16ms to refresh the view), so it can only get the old one. , but the position in the Adapter can get the latest position immediately.

    About this🎜

    reply
    0
  • Cancelreply