dhamini poornachandra
dhamini poornachandra

Reputation: 472

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>

Upvotes: 2

Views: 5545

Answers (2)

dhamini poornachandra
dhamini poornachandra

Reputation: 472

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>

Upvotes: 2

Mahesh Babariya
Mahesh Babariya

Reputation: 4570

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);
    }
});

Upvotes: 1

Related Questions