1

I have ListView. I call scrollBy(0, 100) method in a constructor. When user want to scroll this list by touch event list returns to previous (without scroll) position and after that scrolls normaly. How can I fix this problem?

pstrag
  • 597
  • 4
  • 16
  • AbsListView has a method scrollListBy(pixels_y) , maybe it works better for setting default scroll position. Edit: whoops, didn't notice it's API 19 method... – harism May 23 '14 at 17:26
  • Yes, probably I used setSelectionFromTop. I will check this on Monday and accept your answer then – pstrag Aug 15 '14 at 12:21
  • I have the same problem. I've created a complex ExpandableListView including other ExpandableLists as some of its child elements and scrollListBy or smoothScrollFromTop doesn't work well. The only operation that moves to the correct position that I want is scrollBy (I don't now why), but I've the problem that when user touches the list, it returns to the pre-scroll state. Do you know how I can prevent this using scrollBy? Not the other operations? – drublik Mar 25 '15 at 11:42

1 Answers1

1

If you want to restore ListView state when recreating fragment (or Activity) it is better to use ListViews's onSaveInstanceState()

// Save ListView state
Parcelable state = listView.onSaveInstanceState();

// Set new items
listView.setAdapter(adapter);

// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);

If you want just to scroll ListView programmatically use method setSelectionFromTop(int position, int y). See the answers to this question Maintain/Save/Restore scroll position when returning to a ListView

Community
  • 1
  • 1
Ruslan Mansurov
  • 1,204
  • 16
  • 21