2

I have a ListView with huge amount of data.But when i go to next page on ListView Item Click and come back to ListView. I want the list to be scrolled to the same point that it was previously.Can someone help to achieve this.

I tried this code.List to be scrolled to the same point but immediately come to the first position.

private void populateList() {
        descArray.clear();
        List<All_Post> allDesc = dbhelper.getAllDescriptions();
        for (All_Post all_Post : allDesc)
        {
            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);
            //int firstPosition = listView.getFirstVisiblePosition();
            //listView.setSelection(firstPosition);

            int index = listView.getFirstVisiblePosition();
            View v = listView.getChildAt(0);
            int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
            listView.setSelectionFromTop(index, top);
        }
    }
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
p. ld
  • 585
  • 3
  • 10
  • 23

4 Answers4

3

it is default property of listview.

if you move to next page on ListView Item Click and come back to ListView it will show same position.

just keep one thing in mind that, you should not refresh your listview on backpressed() or when you are coming back to listview or any otherway you are using to moveback to listview .

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • it means does not call this method adapter.notifyDataSetChanged(); – p. ld Jan 07 '16 at 05:30
  • yes, do not call that method. when you are moving back to listivew and it will retain same position. @p.ld – Amit Vaghela Jan 07 '16 at 05:31
  • But this use only for backpressed() and what about other buttons like simple button like previous_button. – p. ld Jan 07 '16 at 05:38
  • on previous_button you must be using backpressed(). so would just say do not refesh listview while getting back to listiview so your position will not change in listview. @p.ld – Amit Vaghela Jan 07 '16 at 05:42
  • I have removed this adapter.notifyDataSetChanged(); and List is working fine , But when i take some button and go to previous list at same position on that click then how to work .But don't understand this on previous_button you must be using backpressed(). – p. ld Jan 07 '16 at 05:47
  • you can use backpressed() to move back but dont code to refresh listview inside that. its done. @p.ld – Amit Vaghela Jan 07 '16 at 05:50
  • it means super.onBackPressed(); this method i have to use in button click method . – p. ld Jan 07 '16 at 05:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100006/discussion-between-p-ld-and-amit-vaghela). – p. ld Jan 07 '16 at 06:00
2

You can save the position of the list view item click in OnItemClickListener of your listView. use that position when you comeback to listView and scroll to that position using

- listView.smoothScrollToPosition (position)
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
1

You can keep track of the position in saveInstanceState and then fetch the same position when you come back to the same activity(if you are destroying the activity) or When you click the item, you can save the position and the use it(if you are not destoying the activity).

Then, once you have the position, you can use:

For direct scrolling, use : getListView().setSelection(position);

For smooth scrolling, use : getListView().smoothScrollToPosition(position);

Ayush
  • 121
  • 2
  • 12
1

try this: public int pos_clicked = -1; on list view item click : pos_clicked = listView.getFirstVisiblePostion();

on come back:if(pos_clicked != -1) listView.setSelection(pos_clicked);

nikk
  • 407
  • 3
  • 7