Problem of repetition

Use Paging3 to load pagination data and display it on LazyColumn of page A, slide up the LazyColumn and navigate. navigate to page B and then navigatUp to page A. The LazyColumn of page A is displayed starting with the first (0) data.

The cause of the problem

The second argument to LazyColumn, state:LazyListState = rememberLazyListState(), can be used to record “status”, but this “status” is not persisted.

The solution

Just create the variable for LazyColumn/LazyRow to record the state and persist it.

I could write it that way

rendering

Parsing LazyListState

  • LazyListState has a constructor that takes arguments, both of which default to 0 and are named as the index and offset of the first visible Item of the list, respectively.

  • LazyListState, there were three of the more important variables scrollPosition, firstVisibleItemIndex, firstVisibleItemScrollOffset, Are the current scroll position, the index to get the current scroll position (observer), and the offset to get the current scroll position (observer)

  • As the user swipes, the update method is constantly triggered, which updates indexState and scrollOffsetState, The observableIndexob and servableScrollOffset get methods get indexState and scrollOffsetState, respectively. That is, the value of the firstVisibleItemIndex, firstVisibleItemScrollOffset.

Also, in the LazyList method called in the LazyColumn, Used to calculate the current of the deviation of the visible items and variable is lazyListState firstVisibleItemIndexNonObservable and lazyListState firstVisibleItemScrollOffsetNonObservabl E, these are the indexes and offsets stored in the updata method of LazyListState.

PS: Calling the LazyListState scrollToItem method can also achieve the above effect, but the resource consumption is a bit more, and the call timing is not easy.

The project address

PlayAndroid