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ū)別解析
相關(guān)文章
Android實現(xiàn)圖片在屏幕內(nèi)縮放和移動效果
這篇文章主要為大家詳細介紹了Android控制圖片在屏幕內(nèi)縮放和移動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02Kotlin by lazy關(guān)鍵字深入探究實現(xiàn)原理
這篇文章主要介紹了by lazy,在kotlin中使用是很常見的,用于實現(xiàn)懶加載某個數(shù)據(jù)。而這兩個單詞不是一體的,其中by是kotlin中的關(guān)鍵字,用于實現(xiàn)委托;lazy是一個方法,他的返回值是委托的具體對象2022-11-11Android實戰(zhàn)項目之實現(xiàn)一個簡單計算器
隨著移動互聯(lián)網(wǎng)的普及,手機應用程序已經(jīng)成為人們生活中不可或缺的一部分,計算器是一類被廣泛使用的應用程序之一,這篇文章主要給大家介紹了關(guān)于Android實戰(zhàn)項目之實現(xiàn)一個簡單計算器的相關(guān)資料,需要的朋友可以參考下2023-10-10ubuntu下 AndroidStudio4.1啟動報錯問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動報錯問題的解決,本文給大家分享個人經(jīng)驗對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10android 仿微信demo——微信消息界面實現(xiàn)(移動端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06Android RecyclerView藝術(shù)般的控件使用完全解析
這篇文章主要介紹了Android RecyclerView藝術(shù)般的控件使用完全解析的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07