如何在滑动时刷新布局以刷新 android

how to refresh layout in on swipe to refresh android

我每次尝试在顶部滚动时都会在 swipeRefresh 中有一个回收器视图调用滑动刷新方法并刷新布局,但我只想在滚动到屏幕顶部时刷新布局.有人知道这个问题的答案吗

感谢任何帮助,谢谢

我的布局如下图

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar"
            android:scrollbars="vertical" />
    </RelativeLayout>

</android.support.v4.widget.SwipeRefreshLayout>

使用 SwipeRefreshLayout 的 setOnRefreshListener 方法

upload_swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            webServiceCall();
            upload_swipe_refresh_layout.setRefreshing(false);
        }
    });

继续刷新

upload_swipe_refresh_layout.post(new Runnable() {
    @Override
    public void run() {
        upload_swipe_refresh_layout.setRefreshing(true);
    }
});

我解决了我的问题。我不得不重新安排我的代码以使其正常工作。 现在布局在到达屏幕顶部时刷新。

<RelativeLayout
    android:id="@+id/rel"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/swipe_container"
            android:scrollbars="vertical" />

    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>