getItemPosition应何时考虑项目位置的变化? [英] When should getItemPosition consider changes of an item's position?

查看:64
本文介绍了getItemPosition应何时考虑项目位置的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的PagerAdapter类中的getItemPosition方法的文档指出它是:

The documentation for the method getItemPosition in Android's PagerAdapter class states that it is:

在宿主视图试图确定项目位置是否已更改时调用.

Called when the host view is attempting to determine if an item's position has changed.

应该调用此方法来指示该适配器在项目组中某个项目的位置是否已更改.

This method is supposed to be called to indicate whether or not an item's position in the group of items for that adapter has changed.

但是,它从未声明覆盖方法何时应考虑更改位置.这是否意味着该位置与上次调用getItemPosition项时的位置不同?这是否意味着项目的位置与上次调用notifyDataSetChanged的位置不同?还是这意味着该项目的位置与将其添加到viewPager时的位置不同?

However, it never states when an overriding method should consider the position to be changed. Does it mean the position is different to the position it was in last time the item getItemPosition was called? Does it mean the item's position is different to last time notifyDataSetChanged was called? Or does it mean that the item's position is different to when the item was added to the viewPager?

推荐答案

您可以在ViewPager.java中看到,仅在 dataSetChanged 中调用 getItemPosition .这意味着,如果项目的位置与上次调用 dataSetChanged 的时间不同,则该位置会发生变化.

You can see in ViewPager.java that getItemPosition is called only in dataSetChanged. This means the item's position changed if it's different than the last time dataSetChanged was called.

我不必担心已更改"的含义;有两种情况:

I would not worry so much about the meaning of "changed"; there are two cases:

  1. 添加的项目的位置永远不会改变,然后 getItemPosition 返回 POSITION_UNCHANGED ;
  2. 项目的位置更改,或项目被删除.如果项目位置没有变化,则返回 POSITION_UNCHANGED 或实际位置没有什么不同.因此,为了简化实现,您可以返回位置(或 POSITION_NONE ),而不必考虑 POSITION_UNCHANGED ..
  1. The position of the added items never changes, then getItemPosition returns POSITION_UNCHANGED;
  2. The position of the items changes, or items are removed. If the item position did not change, there is no difference if you return POSITION_UNCHANGED or the actual position. So to keep the implementation simple you can return the position (or POSITION_NONE) and forget about POSITION_UNCHANGED..

这篇关于getItemPosition应何时考虑项目位置的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆