Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwipeRefreshLayout and refresh state

I am struggling with a documentation in SwipeRefreshLayout of Google's support library.

When I receive the refresh call using the callback set by setOnRefreshListener, I do my action and after that, I found that I need to set the refresh state to false again - if I do NOT do that, I can never trigger it again by swiping!

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    if (mSwipeRefreshLayout != null) {

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(getApplication(), "Refreshing!", Toast.LENGTH_SHORT).show();
                //requestDataRefresh();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

So far, so good. But look at the documentation of setRefreshing:

/**
 * Notify the widget that refresh state has changed. Do not call this when
 * refresh is triggered by a swipe gesture.
 *
 * @param refreshing Whether or not the view should show refresh progress.
 */
public void setRefreshing(boolean refreshing) {...

But setting the refresh state to false again is exactly what I have to do after refreshing!

Is the documentation wrong or am I seeing it wrong? Shouldn't the doc rather say: "Do not call this with refreshing state true when refresh is triggered by a swipe gesture"??

What do you think?

like image 976
Zordid Avatar asked Sep 29 '22 06:09

Zordid


2 Answers

When you swipe SwipeRefreshLayout from top to bottom, setRefreshing(true) is triggered automatically. So you don't need to call setRefreshing(true) repeatedly. Meanwhile, if you call setRefreshing(true) but not swipe SwipeRefreshLayout, you will see the same scene when you swipe SwipeRefreshLayout. And no matter whether you call setRefreshing(true) or swipe SwipeRefreshLayout from top to bottom, you need to call setRefreshing(false) after you finish your task. Just like you did in your code segment above.

like image 125
SilentKnight Avatar answered Oct 06 '22 18:10

SilentKnight


I think it should probably say that there is no need to call this with true if you're refresh was triggered with the swipe gesture.

Incidentally, I found some issues when updating the UI in certain cases which were solved by wrapping the call like this:

private void setRefreshing(final boolean refreshing) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(refreshing);
                }
            });
        }
    }
like image 41
Damian Avatar answered Oct 06 '22 18:10

Damian