当我滑动刷新 SwipeRefreshLayout 的动画显示时,但它在 onRefresh() 运行时被卡住,并且只有在 onRefresh() 完成时才开始旋转。
主要 Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFAB = (FloatingActionButton) findViewById(R.id.myFAB);
recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
callCadastroEncoemnda();
callPackageList();
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(
android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
刷新:
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
Update.updateAll(MainActivity.this);
//Check if the data change
if (!packages.equals(dao.getPackages())) {
adapter.updateList(dao.getPackages());
}
swipeRefreshLayout.setRefreshing(false);
}
最佳答案
解决了!
我已将刷新代码放在 Runnable 上:
(我也改变了我的刷新)
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (Update.updateAll(MainActivity.this)) {
packages = dao.getPackages(config.getFilter());
packagesAux = dao.getPackages(config.getFilter());
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.animateTo(packages);
}
});
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}