Android GridView implements the method of scrolling to the specified location

  • 2020-06-23 01:53:16
  • OfStack

When there are many items in a list, and each item can enter other Activity or Fragment, the location of the previous list is a good function.

Today I looked at how to save the browse location and found four related methods in GridView and its parent class:


public void smoothScrollToPosition (int position)

Scroll to the location specified by position, which is available under api level 11

* When you have finished scrolling, you return to the list, which works fine, but when you return from the list again, the scroll fails, which means you can only scroll once, which I think is 1 Bug. Then I tested it on ROM 2.3 and it didn't roll properly.


public void smoothScrollToPosition (int position, int boundPosition)

Scroll to the location specified by position, then determine if position+boundPosition exceeds the total number of items in Adapter, and if so, back up

Total -boundPosition location displayed.

* The problem is the same as the one above


public void smoothScrollToPositionFromTop (int position, int offset, int duration)

Scroll to the position project location and the position project distance above GirdView is offset pixels. duration specifies the time (milliseconds) required to scroll

* There is no problem with rolling only once, but the lowest api level requires 11.


public void smoothScrollToPositionFromTop (int position, int offset)

The effect is the same as method 3, but api is used by default, with a minimum of api level 11.

The other:

ViewPager can use setCurrentItem(int position) to set which Fragment is currently displayed.


Related articles: