Android: How to match the scroll extent of list in one activity to that one that belongs to another activity

StackOverflow https://stackoverflow.com/questions/23544437

  •  18-07-2023
  •  | 
  •  

Question

I have two activities, and in each, I have a list using adapters.

I would like to add this as a skill:

to match the scroll state of both the lists (ListView(s)).

Say in the activity 1, I have list of 50 animals, and in the activity two I have the list of 50 food items each animal in activity likes to eat (one to one).

Now when I select 30th animal in first activity, and the second activity opens with not only the 30th item is selected (Selector), but the scroll state is also same. Then again On the second activity, I choose 2nd food item and when returning to the first activity, not only the 2nd animal is selected, and highlighted, it is scrolled to that item as well.

Was it helpful?

Solution

There are two methods associated with the focus management of a list. You have to be sure that there is no onSelectedListener as well on the list so that there is no unwanted trigger of any activity.

  1. list.setSelection(index); // just move to the index.
  2. list.smoothScrollToPosition(index); // scroll till the index is reached.

record the index in the parcel in in on saveInstanceState, for one activity and pass in bundle to the target activity, in both the direction of navigation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top