以编程方式触发SwipeRefreshLayout触发器
有没有办法以编程方式触发SwipeRefreshLayout
? animation应该开始, OnRefreshListener
接口的onRefresh
方法应该被调用。
如果你正在使用5.0中新的swipeRefreshLayout
如上图所示,您只需添加以下行即可通过编程方式触发刷新刷新
mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); } });
如果你只是打电话
mSwipeRefreshLayout.setRefreshing(true);
它不会触发圆圈animation,所以通过添加上面的行,你只需在UI线程中延迟,以便它显示在UI线程内的圆形animation。
通过调用mSwipeRefreshLayout.setRefreshing(true)
, OnRefreshListener
将不会被执行
为了停止循环加载animation,调用mSwipeRefreshLayout.setRefreshing(false)
为了触发SwipeRefreshLayout我试过这个解决scheme:
SwipeRefreshLayout.OnRefreshListener swipeRefreshListner = new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Log.i(TAG, "onRefresh called from SwipeRefreshLayout"); // This method performs the actual data-refresh operation. // The method calls setRefreshing(false) when it's finished. loadData(); } };
现在关键部分:
swipeLayout.post(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(true); // directly call onRefresh() method swipeRefreshListner.onRefresh(); } });
您可以通过编程方式调用onRefresh()方法,然后在方法内部启动animation(如果尚未启动)。 请参阅以下内容:
@Override public void onRefresh() { if (!mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(true); //TODO }
只是强迫join这两个可以滑动的手势
swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Log.i(TAG, "onRefresh called from SwipeRefreshLayout"); // This method performs the actual data-refresh operation. // The method calls setRefreshing(false) when it's finished. FetchData(); } } );