0

I am opening new activity on list view item click,I press back button I want set List view in same position. so for example I am clicking 25 item in list view,when I come back list view is show first element I want to show 25 item in list view.

sai
  • 2,562
  • 8
  • 31
  • 46

6 Answers6

2

Use a Fragment to wrap your Listview. set setRetainInstance(true) so all Attributes are restored atomatically after restoring the instance. You don't need to use Bundles again. Then save the position of the first visible item and the offset to restore the exact position. (if you are not using Fragments you need to store top and index in the bundle)

private int top;
private int index; 

@Override onPause(){
  int index = mList.getFirstVisiblePosition();
  View v = mList.getChildAt(0);
  int top = (v == null) ? 0 : v.getTop();
  super.onPause();
}

@Override onResume(){
  super.onResume();
  mList.setSelectionFromTop(index, top);
}

have a look here

Community
  • 1
  • 1
Simulant
  • 19,190
  • 8
  • 63
  • 98
2

store the position and set it on back key pressed

@Override

public boolean onKeyDown (int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {
    listview.setSelection(position);
    return true;
}

return super.onKeyDown(keyCode, event);
}
Navaneeth
  • 883
  • 8
  • 11
  • data of list is not come at a time when scrolling list view to bottom is getting data from server like 10 10 record – sai Dec 20 '13 at 12:49
0

you can store last click position somewhere and on back press ....in onresume() ...do this..

For a direct scroll:

getListView().setSelection(21);

For a smooth scroll:

getListView().smoothScrollToPosition(21);
nitesh goel
  • 6,338
  • 2
  • 29
  • 38
0

When traversed from a listview onPause() method of the source activity is called.You can maintain the state of the listview by adding following line to the end:

index = list.getFirstVisiblePosition();

in the on resume(onResume()) of your source activity , add this line at the end:

list.setSelectionFromTop(index, 0);

That should do the trick.

Rajesh Wadhwa
  • 1,016
  • 2
  • 8
  • 14
  • data of list is not come at a time when scrolling list view to bottom is getting data from server like 10 10 record – sai Dec 20 '13 at 12:49
0

Use the ListView.setSelection(int position) method. It will select the list item at the specified position, but only if not in touch mode. If in touch mode, it will just scroll to the specified position so that the item at position is the first on the screen.

Amit Gupta
  • 1,067
  • 13
  • 26
0

In onPause() method:

index = listView.getFirstVisiblePosition();

In onResume() method:

listView.setSelection(index);

Besides this, you must confirm that: setSelection(int position) method is invoked after setAdapter() of listView, or listView will be reset.

lutaoact
  • 4,149
  • 6
  • 29
  • 41