0

I'm using SwipeRefreshLayout and want to set it refreshing after I create the Activity, but the setRefreshing(true) do not work. So I find this question in SO:SwipeRefreshLayout setRefreshing not showing indicator initially.

I choose to override SwipeRefreshLayout to store local refreshing flag:

public class MyRefreshLayout extends SwipeRefreshLayout {

    public MyRefreshLayout(Context context) {
        super(context);
    }

    public MyRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private boolean mMeasured = false;
    private boolean mPreMeasureRefreshing = false;

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!mMeasured) {
            mMeasured = true;
            setRefreshing(mPreMeasureRefreshing);
        }
    }


    @Override
    public void setRefreshing(boolean refreshing) {
        if (mMeasured) {
            super.setRefreshing(refreshing);
        } else {
            mPreMeasureRefreshing = refreshing;
        }
    }
}

It will refresh after I create activity but the OnRefreshListener do not be called. Here is my init code:

mRefreshLayout = (MyRefreshLayout) findViewById(R.id.refresh_layout);
mRefreshLayout.setColorSchemeResources(R.color.holo_blue_dark, R.color.holo_blue_light, R.color.holo_green_light, R.color.holo_green_light);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
          mRefreshLayout.setRefreshing(false);
    }
});

mRefreshLayout.setRefreshing(true);

I can't find relate question , please help , thanks!

Community
  • 1
  • 1
zzy
  • 1,771
  • 1
  • 13
  • 48

2 Answers2

3

I found that I misunderstood the OnRefreshListener. The doc says:

Classes that wish to be notified when the swipe gesture correctly triggers a refresh should implement this interface.

So If I call mRefreshLayout.setRefreshing(true);. It is not a swipe gesture , the listener won't be called!

zzy
  • 1,771
  • 1
  • 13
  • 48
0

You can find the solution in Min2's answer for the solution on how to call the listener

SwipeRefreshLayout trigger programmatically

Community
  • 1
  • 1
kidustiliksew
  • 443
  • 3
  • 11