上面有scrollView和Layout的SwipeRefreshLayout

use*_*752 24 android swiperefreshlayout

我有以下布局

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            //some views here
        </LinearLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*" >
        </TableLayout>

    </LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

问题是当我向下滚动表格时,我无法再次滚动,因为正在触发swipelayout.仅当表的第一个视图可见时,如何触发swiperefresh?

jjn*_*guy 72

我发现,如果您更换ScrollViewandroid.support.v4.widget.NestedScrollView,你指望它的滚动行为会奏效.

  • 如果您已迁移到 androidX,请使用 androidx.core.widget.NestedScrollView (3认同)
  • 简单完美 (2认同)
  • 拯救了我的一天。谢谢! (2认同)
  • 我的问题有点不同.无论何时我向下滑动屏幕,SwipeRefreshLayout都会拾取动作,使其无法向下滚动视图,除非它处于刷新状态.或者我必须首先向上滑动然后向下滑动而不要移开手指.你的解决方案有很多帮助.谢谢. (2认同)

小智 8

自己实现SwipeRefreshLayout并以这种方式覆盖canChildScrollUp:

    @Override
public boolean canChildScrollUp() {
    if (scrollView != null)
        return scrollView.canScrollVertically(-1);

    return false;
}
Run Code Online (Sandbox Code Playgroud)

只需替换ScrollView的任何子类.

  • 没有必要实现自定义“SwipeRefreshLayout”。我们可以在 `SwipeRefreshLayout` 上添加 `setOnChildScrollUpCallback()` 并将我们的逻辑放在回调中。 (2认同)

小智 5

如果你有这样的布局:

<SwipeRefreshLayout>
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/your_scroll_view_id">
        <LinearLayout>
        ...
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

您需要创建自己的类并以这种方式覆盖该函数:

class SwipeRefreshLayoutCustom extends SwipeRefreshLayout {
    public SwipeRefreshLayoutCustom(Context context, AttributeSet attributes) {
        super(context, attributes)
    }
    @override
    boolean canChildScrollUp() {
        return your_scroll_view_id.scrollY != 0
    }
}
Run Code Online (Sandbox Code Playgroud)