简体   繁体   中英

how to refresh layout in on swipe to refresh android

I have a recycler view inside a swipeRefresh every time i try to scroll on top the swipe to refresh method gets called and the layout is refreshed, but i want to refresh the layout only on scrolling to the top of the screen. Does anybody have an answer to this question.

Any help is appreciated, Thank you

My layout is as shown below.

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

I solved my problem. I had to rearrange my code to get it working. Now the lyout refreshes on on reaching the top of the screen.

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

Use setOnRefreshListener method of SwipeRefreshLayout

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

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

For keep refreshing

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM