Android實(shí)現(xiàn)SwipeRefreshLayout首次進(jìn)入自動(dòng)刷新
看到了Android版知乎實(shí)現(xiàn)了這種效果,就自己也實(shí)現(xiàn)了一下。
先來一張效果圖

實(shí)現(xiàn)方式:
方法一:
①在onWindowFocusChanged()方法中,設(shè)置為刷新狀態(tài)為true
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
②在獲取數(shù)據(jù)完成后設(shè)置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
方法二:
①調(diào)用mSwipeRefreshLayout.measure()方法后,設(shè)置刷新狀態(tài)為true
//手動(dòng)調(diào)用,通知系統(tǒng)去測量
mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
②在獲取數(shù)據(jù)完成后設(shè)置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
說明:
方法一和方法二的第一步的目的,都是為了在SwipeRefreshLayout繪制完成之后,再設(shè)置刷新狀態(tài)為true,否則大多數(shù)情況下,SwipeRefreshLayout刷新球會(huì)不顯示。
源碼:
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);
//設(shè)置刷新球顏色
mSwipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.YELLOW);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.parseColor("#BBFFFF"));
//手動(dòng)調(diào)用,通知系統(tǒng)去測量
// mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
getData();
}
/**
* 模擬網(wǎng)絡(luò)請求
*/
private void getData() {
new Thread() {
@Override
public void run() {
super.run();
//模擬網(wǎng)絡(luò)請求
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在UI線程中更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("首次進(jìn)入自動(dòng)刷新");
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
}
}.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SwipeRefreshLayout超詳細(xì)講解
- Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
- Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除
- Android 中SwipeRefreshLayout與ViewPager滑動(dòng)事件沖突解決方法
- android中SwipeRefresh實(shí)現(xiàn)各種上拉,下拉刷新示例
- Android使用Item Swipemenulistview實(shí)現(xiàn)仿QQ側(cè)滑刪除功能
- Android 中 Swipe、Scroll 和 Fling 的區(qū)別解析
相關(guān)文章
Android實(shí)現(xiàn)圖片在屏幕內(nèi)縮放和移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控制圖片在屏幕內(nèi)縮放和移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Kotlin by lazy關(guān)鍵字深入探究實(shí)現(xiàn)原理
這篇文章主要介紹了by lazy,在kotlin中使用是很常見的,用于實(shí)現(xiàn)懶加載某個(gè)數(shù)據(jù)。而這兩個(gè)單詞不是一體的,其中by是kotlin中的關(guān)鍵字,用于實(shí)現(xiàn)委托;lazy是一個(gè)方法,他的返回值是委托的具體對象2022-11-11
Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡單計(jì)算器
隨著移動(dòng)互聯(lián)網(wǎng)的普及,手機(jī)應(yīng)用程序已經(jīng)成為人們生活中不可或缺的一部分,計(jì)算器是一類被廣泛使用的應(yīng)用程序之一,這篇文章主要給大家介紹了關(guān)于Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡單計(jì)算器的相關(guān)資料,需要的朋友可以參考下2023-10-10
Android實(shí)現(xiàn)輕量線性與百分比圖表的方法
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)輕量線性與百分比圖表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Android實(shí)現(xiàn)城市選擇三級聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)城市選擇三級聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
ubuntu下 AndroidStudio4.1啟動(dòng)報(bào)錯(cuò)問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動(dòng)報(bào)錯(cuò)問題的解決,本文給大家分享個(gè)人經(jīng)驗(yàn)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
android 仿微信demo——微信消息界面實(shí)現(xiàn)(移動(dòng)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06
Android中實(shí)現(xiàn)GPS定位的簡單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)GPS定位的簡單例子,例子邏輯清晰,但相對簡單了些,需要的朋友可以參考下2014-07-07
Android RecyclerView藝術(shù)般的控件使用完全解析
這篇文章主要介紹了Android RecyclerView藝術(shù)般的控件使用完全解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

