首页 > 解决方案 > NestedScrollView 内的 RecyclerView 在 NotifyDataSetChanged 后继续滚动 - 问题

问题描述

我在 NestedScrollView 中添加了 RecyclerView。在第一次尝试从服务器获取数据并在 UI 上呈现时,还添加了滚动侦听器。这对我来说很好。

问题是当我滚动到 NestedScrollView 的底部时,另一个服务器调用以获取更多数据,将数据添加到列表后,我调用了 notifydataSetchanged。但是这次刷新数据后 NestedScrollView 继续自动滚动。

看起来像:
1)最后滚动到底部首先暂停
2)在服务器 API 调用后刷新列表
3)暂停的滚动,现在自动恢复。

这是我的滚动监听器:

 nested_scroll_layout.getViewTreeObserver().addOnScrollChangedListener(new 
   ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            View view = 
    nested_scroll_layout.getChildAt(nested_scroll_layout.getChildCount() - 1);

            int diff = (view.getBottom() - (nested_scroll_layout.getHeight() 
    + nested_scroll_layout
                    .getScrollY()));

            if (diff == 0 && !isCommentaryLoading) {
                isCommentaryLoading = true;
                hitCommentryApi(true);
            }
        }
    });

=======================================

API 响应成功后,将数据添加到当前列表中。

then `commentaryAdapter.notifyDataSetChanged();`

标签: android

解决方案


推荐阅读