Android下拉刷新SwipeRefreshLayout简单用法
生活随笔
收集整理的這篇文章主要介紹了
Android下拉刷新SwipeRefreshLayout简单用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前一直都想用下拉刷新,感覺上是龐大的工程,所以擱置了。現(xiàn)在學習了一下其實真的超級簡單。
看了《第一行代碼》以及 https://www.jianshu.com/p/3c402a9e4b7d文章
看上去是真的簡單。SwipeRefreshLayout下嵌套一個控件
1.布局代碼
<android.support.v4.widget.SwipeRefreshLayoutandroid:id="@+id/mswipeRefreshLayout"android:layout_width="match_parent"android:layout_height="match_parent" ><WebViewandroid:id="@+id/mwebview"android:layout_width="match_parent"android:layout_height="match_parent"></WebView> </android.support.v4.widget.SwipeRefreshLayout>混編的app,這里嵌套了一個webview
2.Activity代碼
mSwipe = findViewById(R.id.mswipeRefreshLayout);private void setSwipe() {/** 設置下拉刷新球顏色*/mSwipe.setColorSchemeResources(R.color.swipeColor1,R.color.swipeColor2,R.color.swipeColor3,R.color.swipeColor4,R.color.swipeColor5);/** 設置下拉刷新的監(jiān)聽*/mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {//刷新需執(zhí)行的操作}}); }?我們可以在res/values/colors.xml文件中設置我們想要的顏色
<color name="swipeColor1">#3F51B5</color> <color name="swipeColor2">#000000</color> ......加載完畢后需要關(guān)閉下拉刷新球
mSwipe.setRefreshing(false);
3.打開頁面自動刷新
mSwipe.measure(0,0);mSwipe.setRefreshing(true);
在onMeasure之前 單獨使用setRefreshing(true)是沒有效果的
?
還可以復寫onWindowFocusChanged當頁面獲得焦點的時刷新
@Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);mSwipe.setRefreshing(true);}?
也可以放到ui線程消息循環(huán)中排隊
mSwipe.post(new Runnable() {@Overridepublic void run() {mSwipe.setRefreshing(true);}});?
在刷新結(jié)束后關(guān)閉刷新球
public void SwipeIsFinish(){if (mSwipe.isRefreshing()) {mSwipe.setRefreshing(false);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/Steffen-xuan/p/11196474.html
總結(jié)
以上是生活随笔為你收集整理的Android下拉刷新SwipeRefreshLayout简单用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS基本语法
- 下一篇: Android平台发展史