上面有scrollView和Layout的SwipeRefreshLayout

问题描述 投票:21回答:4

我有以下布局

<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>

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

android swiperefreshlayout
4个回答
63
投票

我发现,如果用ScrollView替换你的android.support.v4.widget.NestedScrollView,滚动行为将按预期工作。


8
投票

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

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

    return false;
}

只需替换ScrollView的任何子类。


3
投票

如果您有这样的布局:

<SwipeRefreshLayout>
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/your_scroll_view_id">
        <LinearLayout>
        ...
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</SwipeRefreshLayout>

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

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

0
投票

使用NestedScrollView:

     app:layout_behavior="@string/appbar_scrolling_view_behavior"
© www.soinside.com 2019 - 2024. All rights reserved.