当 ScrollView 不在顶部时禁用 SwipeRefreshLayout

Posted

技术标签:

【中文标题】当 ScrollView 不在顶部时禁用 SwipeRefreshLayout【英文标题】:Disable SwipeRefreshLayout when the ScrollView is not on the top 【发布时间】:2016-01-10 21:15:26 【问题描述】:

我有一个Fragment,其中有一个ScrollView 和一个SwipeRefreshLayout。当ScrollView 不在顶部时,我想禁用SwipeRefreshLayout,以便用户可以在Fragment 的顶部再次滚动。我尝试创建一个自定义ScrollView 并像这样覆盖onScrollChanged 方法

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
    View view = (View) getChildAt(getChildCount() - 1);
    int d = view.getBottom();
    d -= (getHeight() + getScrollY());
    if (d <= 0) 
        // bottom
     else if (getScrollY() <= 0) 
        // top
        swipeRefreshLayout.setEnabled(true);
     else 
        swipeRefreshLayout.setEnabled(false);
        super.onScrollChanged(l, t, oldl, oldt);
    

这适用于纵向模式,但不适用于横向模式。我还尝试检查 getScrollX() &lt;= 0 是否无效。

【问题讨论】:

如果用户不在ScrollView的顶部,如何触发刷新? 他回到顶部并滑动。 【参考方案1】:

尝试使用NestedScrollView

当需要在另一个滚动视图中使用滚动视图时使用此选项。

【讨论】:

以上是关于当 ScrollView 不在顶部时禁用 SwipeRefreshLayout的主要内容,如果未能解决你的问题,请参考以下文章