When should getItemPosition consider changes of an item's position?

后端 未结 2 2010
北恋
北恋 2021-02-14 01:02

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

Called when the host view is attempting to determine

2条回答
  •  被撕碎了的回忆
    2021-02-14 01:41

    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. 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..

提交回复
热议问题