使用 Google 官方 SwipeRefreshLayout 時,下拉刷新動畫偶爾無限轉動,無法自動消失,手動也無法停止。
網絡請求完成後未調用 setRefreshing(false),或在子線程直接操作 UI 導致更新失效。
public void onRefresh() {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
// 模擬網絡請求完成
mSwipeLayout.setRefreshing(false); // 必須在主線程調用
}, 5000);
}
setRefreshing(false)。runOnUiThread、Handler 或 LiveData 觀察)。postDelayed 強制關閉動畫防止死轉。