java bindview_java – onBindViewHolder()中的constructor.getAdapterPosition()与构造函数的position属性有什么区别[复制]

编程知识 更新时间:2023-05-02 19:01:41

public void onBindViewHolder(myViewHolder holder, int position) {

//1. details obj = list.get(holder.getAdapterPosition());

//2. details obj = list.get(position);

holder.position = position;

}

我收到了警告

Do not treat position as fixed; only use immediately and call

holder.getAdapterPosition() to look it up later RecyclerView will not

call onBindViewHolder again when the position of the item changes in

the data set unless the item itself is invalidated or the new position

cannot be determined. For this reason, you should only use the

position parameter while acquiring the related data item inside this

method, and should not keep a copy of it. If you need the position of

an item later on (e.g. in a click listener), use getAdapterPosition()

which will have the updated adapter position later.

所以我对1和2感到困惑,我应该选择哪个?为什么?正如它说getAdapterPosition()提供更新的位置,我从列表中获取基于位置的值.

谢谢.

解决方法:

你得到的警告不是关于使用position或getAdapterPosition().这是关于保存位置:

holder.position = position;

您不需要在持有者中保存头寸,因为它的位置可以改变,您可以通过调用holder.getAdapterPosition();

来自文档:

Note that unlike ListView, RecyclerView will not call this method

again if the position of the item changes in the data set unless the

item itself is invalidated or the new position cannot be determined.

For this reason, you should only use the position parameter while

acquiring the related data item inside this method and should not keep

a copy of it. If you need the position of an item later on (e.g. in a

click listener), use getAdapterPosition() which will have the updated

adapter position.

关于使用哪个:

如果在onBindViewHolder方法中调用,则两者都应返回相同的结果.我建议使用位置,因为它是最简单和最安全的.

标签:android,java

来源: https://codeday.me/bug/20190727/1554235.html

更多推荐

java bindview_java – onBindViewHolder()中的constructor.getAdapterPosition()与构造函数

本文发布于:2023-04-28 04:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/f486abbe5abfa43438195372bf80eea8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   函数   属性   onBindViewHolder   bindview_java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 107592文章数
  • 27226阅读数
  • 0评论数