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 Handler內(nèi)存泄漏原因及解決方案
這篇文章主要介紹了Android Handler內(nèi)存泄漏原因及解決方案,幫助大家更好的理解和利用Android進(jìn)行開發(fā),感興趣的朋友可以了解下2021-02-02
android里TextView加下劃線的幾種方法總結(jié)
下面小編就為大家?guī)硪黄猘ndroid里TextView加下劃線的幾種方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Android實(shí)現(xiàn)調(diào)用系統(tǒng)相冊和拍照的Demo示例
這篇文章主要介紹了Android實(shí)現(xiàn)調(diào)用系統(tǒng)相冊和拍照的Demo示例,實(shí)例分析了Android調(diào)用系統(tǒng)相冊及拍照的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android手勢密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細(xì)介紹了Android手勢密碼view的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)分享功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)分享功能,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)針對文字、圖片等元素分享功能的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android毛玻璃背景效果簡單實(shí)現(xiàn)代碼
這篇文章主要介紹了Android毛玻璃背景效果簡單實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08
jenkins 遠(yuǎn)程構(gòu)建Android的過程詳解
這篇文章主要介紹了jenkins 遠(yuǎn)程構(gòu)建Android的過程詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09

