Android實現SwipeRefreshLayout首次進入自動刷新
看到了Android版知乎實現了這種效果,就自己也實現了一下。
先來一張效果圖

實現方式:
方法一:
①在onWindowFocusChanged()方法中,設置為刷新狀態(tài)為true
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
②在獲取數據完成后設置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
方法二:
①調用mSwipeRefreshLayout.measure()方法后,設置刷新狀態(tài)為true
//手動調用,通知系統(tǒng)去測量
mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
②在獲取數據完成后設置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
說明:
方法一和方法二的第一步的目的,都是為了在SwipeRefreshLayout繪制完成之后,再設置刷新狀態(tài)為true,否則大多數情況下,SwipeRefreshLayout刷新球會不顯示。
源碼:
package org.raphets.swiperefreshlayoutdemo;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout mSwipeRefreshLayout;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl);
mTextView = (TextView) findViewById(R.id.tv);
//設置刷新球顏色
mSwipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.YELLOW);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.parseColor("#BBFFFF"));
//手動調用,通知系統(tǒng)去測量
// mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
getData();
}
/**
* 模擬網絡請求
*/
private void getData() {
new Thread() {
@Override
public void run() {
super.run();
//模擬網絡請求
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在UI線程中更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("首次進入自動刷新");
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
}
}.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android SwipeRefreshLayout超詳細講解
- Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
- Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
- android使用SwipeRefreshLayout實現ListView下拉刷新上拉加載
- android基于SwipeRefreshLayout實現類QQ的側滑刪除
- Android 中SwipeRefreshLayout與ViewPager滑動事件沖突解決方法
- android中SwipeRefresh實現各種上拉,下拉刷新示例
- Android使用Item Swipemenulistview實現仿QQ側滑刪除功能
- Android 中 Swipe、Scroll 和 Fling 的區(qū)別解析
相關文章
ubuntu下 AndroidStudio4.1啟動報錯問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動報錯問題的解決,本文給大家分享個人經驗對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
android 仿微信demo——微信消息界面實現(移動端)
本系列文章主要介紹了微信小程序-閱讀小程序實例(demo),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06
Android RecyclerView藝術般的控件使用完全解析
這篇文章主要介紹了Android RecyclerView藝術般的控件使用完全解析的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07

