Java SwipeRefreshLayout 以编程方式触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24587925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SwipeRefreshLayout trigger programmatically
提问by Niklas
Is there any way to trigger the SwipeRefreshLayout
programmatically? The animation should start and the onRefresh
method from the OnRefreshListener
interface should get called.
有没有办法以SwipeRefreshLayout
编程方式触发?动画应该开始,并且应该调用接口中的onRefresh
方法OnRefreshListener
。
采纳答案by Ramz
if you are using the new swipeRefreshLayout intoduced in 5.0
如果您使用的是 5.0 中引入的新 swipeRefreshLayout
As the image shown above you just need to add the following line to trigger the swipe refresh layout programmatically
如上图所示,您只需要添加以下行以编程方式触发滑动刷新布局
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
}
});
if you simply call
如果你只是打电话
mSwipeRefreshLayout.setRefreshing(true);
it won't trigger the circle to animate, so by adding the above line u just make a delay in the UI thread so that it shows the circle animation inside the ui thread.
它不会触发圆圈动画,因此通过添加上面的行,您只需在 UI 线程中进行延迟,以便在 ui 线程内显示圆圈动画。
By calling mSwipeRefreshLayout.setRefreshing(true)
the OnRefreshListener
will NOT get executed
通过调用mSwipeRefreshLayout.setRefreshing(true)
的OnRefreshListener
将不会得到执行
In order to stop the circular loading animation call mSwipeRefreshLayout.setRefreshing(false)
为了停止循环加载动画调用 mSwipeRefreshLayout.setRefreshing(false)
回答by Min2
In order to trigger SwipeRefreshLayoutI tried this solution:
为了触发 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();
}
};
Nowkey part:
现在关键部分:
swipeLayout.post(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(true);
// directly call onRefresh() method
swipeRefreshListner.onRefresh();
}
});
回答by Goodlife
Just to force in add this two to ennable swipe gesture
只是为了强制将这两个添加到 ennaable 滑动手势
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();
}
}
);
回答by Goodlife
You can call onRefresh() method programmatically and then inside the method start the animation if it is not already started. See the following:
您可以以编程方式调用 onRefresh() 方法,然后在方法内部启动动画(如果动画尚未启动)。请参阅以下内容:
@Override
public void onRefresh() {
if (!mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(true);
//TODO
}
回答by Aditya Pratama
binding.swipeRefreshLayout.setRefreshing(true); // show loading
binding.swipeRefreshLayout.post(this::updateUI); // call method
binding.swipeRefreshLayout.setOnRefreshListener(this::updateUI); // call method
回答by Indiana
Bit late to the thread, but you do not need to launch a Runnable to do this. You can simply trigger the refresh and call your onRefresh method directly in onCreate, or wherever you want this to happen:
线程有点晚,但您不需要启动 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
}
}