SwipeRefreshLayout with scrollView and Layout above

心不动则不痛 提交于 2019-11-30 12:33:52

问题


I have the following layout

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

The problem is when I scrolldown the table, I can't scrollUp again because the swipelayout is being triggered. How can I trigger the swiperefresh only when the first view of the table is visible?


回答1:


I found that if you replace your ScrollView with a android.support.v4.widget.NestedScrollView the scrolling behavior will work as you expect it to.




回答2:


Make your own implementation of SwipeRefreshLayout and override the canChildScrollUp in this way:

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

    return false;
}

just replace with any subclass of ScrollView.




回答3:


If you have layout like this:

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

You need to create your own class and override the function in this way:

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



回答4:


Use NestedScrollView with:

     app:layout_behavior="@string/appbar_scrolling_view_behavior"


来源:https://stackoverflow.com/questions/25294395/swiperefreshlayout-with-scrollview-and-layout-above

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!