有没有办法在程序中触发 SwipeRefreshLayout
?动画应该会开始,从 OnRefreshListener
接口传来的 onRefresh
方法也应该被调用。
有没有办法在程序中触发 SwipeRefreshLayout
?动画应该会开始,从 OnRefreshListener
接口传来的 onRefresh
方法也应该被调用。
如果你正在使用5.0中引入的新的swipeRefreshLayout
如上图所示,你只需添加以下代码即可通过编程方式触发swipe refresh layout:
在Java中:
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
}
});
Kotlin中的单例模式:
mSwipeRefreshLayout.post { mSwipeRefreshLayout.isRefreshing = true }
如果你在Java中简单地调用:
mSwipeRefreshLayout.setRefreshing(true);
或者在 Kotlin 中
mSwipeRefreshLayout.isRefreshing = true
添加上述代码行不会触发圆形动画,它只是在UI线程中引入一定的延迟,以便在UI线程中显示圆形动画。
调用mSwipeRefreshLayout.setRefreshing(true)
将不会执行OnRefreshListener
。
为了停止循环加载动画,请调用mSwipeRefreshLayout.setRefreshing(false)
。
.setRefreshing(false)
? - Zapnologica为了触发SwipeRefreshLayout,我尝试了这个解决方案:
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();
}
});
// 此方法执行实际的数据刷新操作。
// 当完成操作时,该方法调用setRefreshing(false)。
FetchData();
}
}
);
- GoodlifeSwipeRefreshLayout.OnRefreshListener
并在需要时调用其函数 onRefresh()
:SwipeRefreshLayout srl;
SwipeRefreshLayout.OnRefreshListener refreshListener;
srl = (SwipeRefreshLayout)v.findViewById(R.id.swipeRefreshLayout);
refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Do your stuff here
}
};
srl.setOnRefreshListener(refreshListener);
refreshListener.onRefresh();
有点晚了,但是你不需要启动一个Runnable来完成这个任务。你可以在onCreate或者其他你想要刷新的地方直接触发刷新并且直接调用你的onRefresh方法:
class MyFragment: SwipeRefreshLayout.OnRefreshListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
}
fun initViews() {
swipe_refresh_layout?.apply {
setOnRefreshListener(this@MyFragment)
isRefreshing = true
onRefresh()
}
}
override fun onRefresh() {
// Do my refresh logic here
}
}
binding.swipeRefreshLayout.setRefreshing(true); // show loading
binding.swipeRefreshLayout.post(this::updateUI); // call method
binding.swipeRefreshLayout.setOnRefreshListener(this::updateUI); // call method
@Override
public void onRefresh() {
if (!mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(true);
//TODO
}
只是为了强制添加这两个以启用滑动手势
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();
}
}
);
mSwipeRefreshLayout.setRefreshing(true);
并新建一个我的 AsyncTask 的实例,然后手动执行它。当然,我把它们放在一个名为doRefresh()
的方法中,在onRefresh()
中调用doRefresh()
。 - Xiao Liang