1

I've made custom adapter and call notifyDataSetChanged() then data updates. But after call notifyDataSetChanged() I can't stop scrolling of listview by touch. I looked at android's source code and found that code in file AbsListView.class (which is parent of ListView)

if (!mDataChanged) { ... }

There is a code to stop scrolling ListView on touch event.

So then we call notifyDataSetChanged() we set mDataChanged in true and so can't stop scrolling.

Could u help me to find out how to enable stop scrolling after data change?

karabara
  • 547
  • 6
  • 24

2 Answers2

0

You need to save the scroll position of your ListView and scroll the list till that scroll amount again after the list is refreshed.

To do so, call this method before adapter.notifyDataSetChanged();

private void saveListScrollPosition() {
        // save index and top position
        index = _listview.getFirstVisiblePosition();
        View view = _listview.getChildAt(0);
        top = (view == null) ? 0 : view.getTop();
    }

And after adapter.notifyDataSetChanged();

write the following line -

    // restore list scroll position
    _listview.setSelectionFromTop(index, top);

where, index, top and _listview are all fields accessible to any method.

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • Why do u think it should help? Because in code android checks if (!mDataChanged) { ... } (however I've tryid your code and it did not help) – karabara Mar 19 '12 at 11:53
0

Probably, you are reading data while the ListView calls getItem method. Using an asynchronous data loading (Handler) could be a better idea. You may find more information in this answer.

Anton
  • 560
  • 6
  • 21