6

I have tabView, displaying list in each tab. When I scroll in one tab, switch to another tab and return to previous tab, position is returned to the top instead of displaying previous scrolled position. How do I achieve this ? Need to know how do I use onSaveInstanceState & onRestoreInstanceState to save position and use the saved position in displaying the previous scrolled position.

Thanks in Advance.


Thanks all for your reply. I tried with all the solutions but ran into other issues. Basically the problem i am facing is as follows.

I have a listview as my first activity when I launch my app. When I click on the list item, it launches the tab activity containing 3 tabs. All 3 tabs uses the same activity called ListActivity. But 3 tabs contains different data. My question is how to retain the position of the list when I switch between the tabs. With the above solutions provided, when I change the position in one tab, it affects the remaining tabs as well. For example, if I am at position 6 in first tab, this position will be set for second and third tab as well as I am using the same ListActivity for all 3 tabs. I am not allowed to share the code. So have to type the problem this long. Also number of tabs created are dynamic. It might be 3 or 4 or 5. But all tabs use 1 ListActivity.

Can anyone give me a example how to achieve this. 1. Single ListActivity used in multiple tabs. 2. Retaining the cursor position in tabs without after affecting other tabs.

Your solution provided is appreciated. Thanks in advance.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Avinash
  • 103
  • 1
  • 1
  • 5
  • It sounds like you're creating a new ListView or somehow update the content each time you switch to a tab. Could you kindly post your code? – Michell Bak Aug 23 '11 at 21:10
  • Please see the following post, which may help you: http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview – Xebozone Nov 01 '14 at 08:29

4 Answers4

12

Don't call setAdapter() again on the list view. Do something like this.

if(myListView.getAdapter() == null)
    myListView.setAdapter(new myAdapter(this, R.layout.row, items));

If you need to update your ListView call notifyDataSetChanged() on the adapter.

b_yng
  • 13,987
  • 6
  • 30
  • 35
10

To get the current position of your ListView, you can call

int position = mCatchList.getFirstVisiblePosition();

Then once you navigate back to the tab with that ListView, you can call

mCatchList.setSelection(position);

It will depend on how your code is written to tell if the position will need to be added to the savedInstanceState, or however.

hooked82
  • 6,026
  • 4
  • 38
  • 44
  • Is it possible to get the floating postion? when I do so it jumps to the top of the view position – Kirill Kulakov Aug 22 '12 at 09:03
  • I believe that's possible by getting the top most view and finding out how much is shown on screen. I don't have an example for this, but I'm pretty sure a quick search will bring something up – hooked82 Aug 29 '12 at 18:05
  • I've found that It is possible to save the preasble object of it, and restore it. – Kirill Kulakov Aug 29 '12 at 19:21
  • @KirillKulakov what do you mean by a 'preasble' object? Please explain as I have a similar issue. – Sufian May 23 '13 at 12:58
1

Override onSaveInstanceState to save the position, and set the position back in onRestoreInstanceState. The Bundle is sort of like a HashMap, (pseudo-code):

In onSaveInstanceState:

bundle.putInt("MyTabsPosition", getPosition());

Then in onRestoreInstanceState:

pseudo.setPosition(bundle.getInt("MyTabsPosition"));
CrackerJack9
  • 3,609
  • 1
  • 24
  • 47
0

In listView.setOnItemClickListener, use the saveLastPosition method. And after listview.setAdapter(adapter);call the getLastPosition method

public void saveLastPosition(){
    int position=listView.getLastVisiblePosition();

    SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);    
    SharedPreferences.Editor editor=pref.edit();
    editor.putInt("lastPosition",position);   
    editor.apply();
}

public void getLastPosition(){
    int position=listView.getLastVisiblePosition();

    SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);      
    int lastPosition=pref.getInt("lastPosition",position);
    listView.setSelection(lastPosition);
}
Jeed
  • 9
  • 5
AnAIDE
  • 21
  • 2
  • Adding description and explanation to answer helps the author and other users to understand what you have done or why your answer is helpful... – Vipin Kumar Soni Mar 16 '19 at 06:59