ListView停止滚动不起作用



我制作了自定义适配器并调用notifyDataSetChanged(),然后进行数据更新。但在调用notifyDataSetChanged()后,我无法停止触摸滚动列表视图。我查看了android的源代码,在文件AbsListView.class(它是ListView的父级)中发现了该代码

if (!mDataChanged) { ... }

有一个代码可以在触摸事件时停止滚动ListView。

因此,我们调用notifyDataSetChanged(),将mDataChanged设置为true,因此无法停止滚动。

你能帮我了解如何在数据更改后启用停止滚动吗?

可能是在ListView调用getItem方法时读取数据。使用异步数据加载(Handler)可能是一个更好的主意。你可以在这个答案中找到更多信息。

您需要保存ListView的滚动位置,并在刷新列表后再次滚动列表,直到该滚动量。

为此,请在adapter.notifyDataSetChanged(); 之前调用此方法

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

adapter.notifyDataSetChanged(); 之后

写下一行-

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

其中,indextop_listview都是任何方法都可以访问的字段。

最新更新