【问题标题】:Can't scroll in a ListView in a swipeRefreshLayout无法在 swipeRefreshLayout 的 ListView 中滚动
【发布时间】:2015-01-18 10:07:09
【问题描述】:

我遇到了 SwipeRefreshLayout 的问题。我在布局中有一个列表视图,每次我在列表视图中向上滚动时,都会触发滑动以刷新布局,并且您永远无法滚动回布局的顶部。有人有什么想法吗?

【问题讨论】:

    标签: android swipe-gesture swiperefreshlayout


    【解决方案1】:

    我找到了我的问题的答案。当列表视图中的第一个子项位于列表顶部时,我手动启用 swipeRefreshLayout。

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            @Override
            public void onScroll(AbsListView listView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ?
                                0 : expandableListview.getChildAt(0).getTop();
                refresh.setEnabled((topRowVerticalPosition >= 0));
            }
        });
    

    【讨论】:

    • 谢谢,我对 Api 10 也有同样的问题……它不在较新的 API 中。好像是个bug
    • 相同的解决方案,只是在帖子末尾更容易阅读:yassirh.com/2014/05/how-to-use-swiperefreshlayout-the-right-way
    • 如果 ListView 是 SwipeLayout 的唯一子级,但如果除了 ListView 之外还有其他视图,并且您希望在有触摸和拉入任何其他位置时启用 SwipeLayout,则此方法工作正常除了 ListView,那么这个解决方案就行不通了。
    • @AliAnsari ExpandableListView
    • @AlexandreG 这个解决方案在这里运行良好而且非常清晰!感谢分享。
    【解决方案2】:

    https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

    根据以上链接Android docs

    此布局应作为将因手势刷新的视图的父级,并且只能支持一个直接子级。

    如果ListViewSwipeRefreshLayout 的第一个直接子级,则在滚动回ListView 的顶部之前不会触发刷新。你不需要在onScroll()做任何事情。

    【讨论】:

    • Tt 是视图中唯一的孩子。上述解决方案对我有用。
    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2020-11-19
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多