1

I populate the list-view from Sq-lite db.Now my problem is that when i click on list-view item for going to next page and after that come back to list-view , I want the list to be scrolled to the same point that it was previously.Any idea how to do this.

This is my list-view method in

onCreate()

private void populateList() {
    descArray.clear();
    List<All_Post> allDesc = dbhelper.getAllDescriptions();
    for (All_Post all_Post : allDesc)
    {
        String strInspectorname = all_Post.getStringInspectorname();
        Log.e("strInspectorname "," = " + strInspectorname);
        descArray.add(all_Post);
    }

    if (adapter == null)
    {
        adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
        listView.setAdapter(adapter);
    }
    else if (adapter != null) {

        adapter.notifyDataSetChanged();
        adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray);
        listView.setAdapter(adapter);
    }
} 

I tried this link Maintain/Save/Restore scroll position when returning to a ListView , listView goes alwyas last position .

Community
  • 1
  • 1
p. ld
  • 575
  • 3
  • 9
  • 22

3 Answers3

2

Try this

 // Save the ListView state (= includes scroll position) as a Parceble
Parcelable state = listView.onSaveInstanceState();

// e.g. set new items
listView.setAdapter(adapter);

// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);
Govinda Paliwal
  • 3,171
  • 5
  • 20
  • 43
1

Follow the below steps,

  1. Store your First listview Position which was clicked somewhere in Database/Shared Preference.
  2. Move to your 2nd listview.
  3. When you come back to your 1st listview just simple Get that stored value of your 1st listview position.

4. For a direct scroll:

getListView().setSelection(YOUR_POSITION);

For a smooth scroll:

getListView().smoothScrollToPosition(YOUR_POSITION); 

Hope it will help you.

Sanket Shah
  • 4,448
  • 3
  • 19
  • 40
0

Before going to the next page just store listviews's scroll position by calling either getScrollY/ getScrollX depending on your listview orientation. When you come back again to this page restore stored position by calling either setScrollX/ setScrollY

Rushikesh Talokar
  • 1,397
  • 4
  • 15
  • 29