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

實現(xiàn)方式:
方法一:
①在onWindowFocusChanged()方法中,設置為刷新狀態(tài)為true
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
②在獲取數(shù)據(jù)完成后設置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
方法二:
①調(diào)用mSwipeRefreshLayout.measure()方法后,設置刷新狀態(tài)為true
//手動調(diào)用,通知系統(tǒng)去測量
mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
②在獲取數(shù)據(jù)完成后設置刷新狀態(tài)為false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
說明:
方法一和方法二的第一步的目的,都是為了在SwipeRefreshLayout繪制完成之后,再設置刷新狀態(tài)為true,否則大多數(shù)情況下,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"));
//手動調(diào)用,通知系統(tǒng)去測量
// mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
getData();
}
/**
* 模擬網(wǎng)絡請求
*/
private void getData() {
new Thread() {
@Override
public void run() {
super.run();
//模擬網(wǎng)絡請求
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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android SwipeRefreshLayout超詳細講解
- Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
- Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
- android使用SwipeRefreshLayout實現(xiàn)ListView下拉刷新上拉加載
- android基于SwipeRefreshLayout實現(xiàn)類QQ的側(cè)滑刪除
- Android 中SwipeRefreshLayout與ViewPager滑動事件沖突解決方法
- android中SwipeRefresh實現(xiàn)各種上拉,下拉刷新示例
- Android使用Item Swipemenulistview實現(xiàn)仿QQ側(cè)滑刪除功能
- Android 中 Swipe、Scroll 和 Fling 的區(qū)別解析
相關文章
Android Handler內(nèi)存泄漏原因及解決方案
這篇文章主要介紹了Android Handler內(nèi)存泄漏原因及解決方案,幫助大家更好的理解和利用Android進行開發(fā),感興趣的朋友可以了解下2021-02-02
android里TextView加下劃線的幾種方法總結(jié)
下面小編就為大家?guī)硪黄猘ndroid里TextView加下劃線的幾種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Android實現(xiàn)調(diào)用系統(tǒng)相冊和拍照的Demo示例
這篇文章主要介紹了Android實現(xiàn)調(diào)用系統(tǒng)相冊和拍照的Demo示例,實例分析了Android調(diào)用系統(tǒng)相冊及拍照的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android使用ViewPager實現(xiàn)屏幕滑動效果
這篇文章主要為大家詳細介紹了Android使用ViewPager實現(xiàn)屏幕滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android編程實現(xiàn)調(diào)用系統(tǒng)分享功能示例
這篇文章主要介紹了Android編程實現(xiàn)調(diào)用系統(tǒng)分享功能,結(jié)合實例形式分析了Android實現(xiàn)針對文字、圖片等元素分享功能的相關操作技巧,需要的朋友可以參考下2017-01-01

