SwipeRefreshLayout不会调用刷新侦听器

时间:2015-05-05 02:39:18

标签: android

我使用SwipeRefreshLayout并希望在创建Activity后将其设置为刷新,但setRefreshing(true)不起作用。所以我在SO中找到了这个问题:SwipeRefreshLayout setRefreshing not showing indicator initially

我选择覆盖SwipeRefreshLayout来存储本地刷新标志:

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

我会在创建活动后刷新,但不会调用OnRefreshListener。这是我的初始代码:

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

我无法找到相关问题,请帮助,谢谢!

2 个答案:

答案 0 :(得分:3)

我发现我误解了OnRefreshListener。医生说:

  

希望在滑动手势正确触发刷新时收到通知的类应该实现此接口。

所以如果我打电话给mRefreshLayout.setRefreshing(true);。这不是一个轻扫手势,听众不会被召唤!

答案 1 :(得分:0)

你可以在Min2的答案中找到关于如何调用监听器的解决方案

SwipeRefreshLayout trigger programmatically