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

后端 未结 2 2015
北恋
北恋 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:33

    By default, the positions of items in a ViewPager are deemed fixed; thats why getItemPosition() by default returns POSITION_UNCHANGED. When you move an item around, ViewPager has to know where to remove an item (the old position of the item) and where to add it. This is what getItemPosition() is used for. It allows you to tell the ViewPager which item to put where, even after your pages have been instantiated.

    getItemPosition() is only called when you call notifyDataSetChanged() on your PagerAdapter. By design this means that the "changed" means "changed from when the ViewPager last populated its pages", as notifyDataSetChanged() causes the ViewPager to redraw its children where necessary. In other words: "changed" has either of the meanings you mentioned; whichever one is the most recent to occur.

    I think getItemPosition is quite tricky to understand without a sample. See my example of how to use getItemPosition(...) here; I think it'll clarify some things.

提交回复
热议问题