比如我想用SwipeRefreshLayout在界面第一次加载进来时先自动刷新一次。
private SwipeRefreshLayout mRefreshLayout;
mRefreshLayout.setRefreshing(true);
这样不起作用,显示不出加载条。
但是交给handler就可以?
private SwipeRefreshLayout mRefreshLayout;
mHandler.post(new Runnable() {
@Override
public void run() {
mRefreshLayout.setRefreshing(true);
}
});
First repost an answer from Zhihu, here explains why calling setRefreshing in onCreate is invalid:
Author: Xiaobo
Link: https://www.zhihu.com/question/35422150/answer/62784161
Source: Zhihu
Copyright All rights reserved to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.
Since the initialization of the view has not been completed when we call setRefresh, the easiest way is to use post to join the queue, just write a demo, and the code is:
It should be noted that setRefreshing only displays animation. If you want to refresh automatically, you must manually call onRefresh; manually calling onRefresh must be in the runnable of the post, located after setRefreshing. Otherwise, if the task in onRefresh is executed too fast, the refresh animation cannot be canceled. situation.
It should be related to the life cycle of Activity. Placing it in the Handler can ensure that the page is refreshed correctly after initialization.
SwipeRefreshLayout refresh will not work before it is fully loaded. You can take a look at the source code.