Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
SwipeRefreshLayout(這個(gè)控件),我先跟大家介紹一下這個(gè)控件:
一、SwipeRefreshLayout簡單介紹
•先看以下官方文檔,已有了很詳細(xì)的描述了。

官方文檔說明
•這里我再大概解釋一下:
•在豎直滑動(dòng)時(shí)想要刷新頁面可以用SwipeRefreshLayout來實(shí)現(xiàn)。它通過設(shè)置OnRefreshListener來監(jiān)聽界面的滑動(dòng)從而實(shí)現(xiàn)刷新。也可以通過一些方法來設(shè)置SwipeRefreshLayout是否可以刷新。如:setRefreshing(true),展開刷新動(dòng)畫。
setRefreshing(false),取消刷新動(dòng)畫。setEnable(true)下拉刷新將不可用。
•使用這個(gè)布局要想達(dá)到刷新的目的,需要在這個(gè)布局里包裹可以滑動(dòng)的子控件,如ListView等,并且只能有一個(gè)子控件。
•介紹總結(jié):使用SwipeRefreshLayout可以實(shí)現(xiàn)下拉刷新,前提是布局里需要包裹一個(gè)可以滑動(dòng)的子控件,然后在代碼里設(shè)置OnRefreshListener設(shè)置監(jiān)聽,最后在監(jiān)聽里設(shè)置刷新時(shí)的數(shù)據(jù)獲取就可以了。由于是新出來的東西,所以要想使用,先把support library的版本升級到19.1或更新。
二、SwipeRefreshLayout主要方法介紹
翻看官方的文檔,可以看到方法有很多,這里只介紹五個(gè)經(jīng)常用到的方法。
•isRefreshing()
•判斷當(dāng)前的狀態(tài)是否是刷新狀態(tài)。
•setColorSchemeResources(int... colorResIds)
•設(shè)置下拉進(jìn)度條的顏色主題,參數(shù)為可變參數(shù),并且是資源id,可以設(shè)置多種不同的顏色,每轉(zhuǎn)一圈就顯示一種顏色。
•setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)
•設(shè)置監(jiān)聽,需要重寫onRefresh()方法,頂部下拉時(shí)會(huì)調(diào)用這個(gè)方法,在里面實(shí)現(xiàn)請求數(shù)據(jù)的邏輯,設(shè)置下拉進(jìn)度條消失等等。
•setProgressBackgroundColorSchemeResource(int colorRes)
•設(shè)置下拉進(jìn)度條的背景顏色,默認(rèn)白色。
•setRefreshing(boolean refreshing)
•設(shè)置刷新狀態(tài),true表示正在刷新,false表示取消刷新。
那么話就不多說了,直接開始了:
效果圖:

activity_listview布局文件
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/sr1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
Activity代碼(ListViewActivity)
public class ListViewActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private List<String> list;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sr1);
swipeRefreshLayout.setOnRefreshListener(this);
list = new ArrayList<>();
for(int i = 0;i<10;i++){
list.add("這是第"+i+"個(gè)數(shù)據(jù)");
}
listView = (ListView) findViewById(R.id.lv);
adapter = new ArrayAdapter(this
, android.R.layout.simple_list_item_1
, android.R.id.text1
, list);
listView.setAdapter(adapter);
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
adapter.clear();
list.add("這是第11個(gè)數(shù)據(jù)");
adapter.notifyDataSetChanged();
}
}, 1000);
}
}
總結(jié)
以上所述是小編給大家介紹的Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android SwipeRefreshLayout超詳細(xì)講解
- 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實(shí)現(xiàn)SwipeRefreshLayout首次進(jìn)入自動(dòng)刷新
- Android 中 Swipe、Scroll 和 Fling 的區(qū)別解析
相關(guān)文章
Android動(dòng)畫實(shí)現(xiàn)原理和代碼
這篇文章主要介紹了Android動(dòng)畫實(shí)現(xiàn)原理和代碼分析,如果你對此感興趣,跟著小編學(xué)習(xí)下吧。2017-12-12
在Android應(yīng)用中實(shí)現(xiàn)離線數(shù)據(jù)同步的步驟詳解
在構(gòu)建 Android 應(yīng)用時(shí),離線數(shù)據(jù)同步是一個(gè)不可或缺的環(huán)節(jié),無論是網(wǎng)絡(luò)狀況不佳,還是用戶處于飛行模式,離線數(shù)據(jù)同步都能讓用戶在無網(wǎng)絡(luò)的情況下繼續(xù)使用應(yīng)用,本文將詳細(xì)介紹如何在 Android 應(yīng)用中實(shí)現(xiàn)離線數(shù)據(jù)同步,需要的朋友可以參考下2024-08-08
Android實(shí)現(xiàn)的數(shù)字格式化用法示例
這篇文章主要介紹了Android實(shí)現(xiàn)的數(shù)字格式化用法,結(jié)合實(shí)例形式分析了Android數(shù)學(xué)運(yùn)算中數(shù)字格式化輸出的相關(guān)技巧,需要的朋友可以參考下2016-08-08
Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2017-01-01
Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法
這篇文章主要介紹了Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法,用idegen來生成針對AndroidStudio或IntelliJ?IDEA的Android系統(tǒng)源代碼工程配置文件,需要的朋友可以參考下2022-08-08
Android開發(fā)中使用顏色矩陣改變圖片顏色,透明度及亮度的方法
這篇文章主要介紹了Android開發(fā)中使用顏色矩陣改變圖片顏色,透明度及亮度的方法,涉及Android針對圖片的讀取、運(yùn)算、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
使用Chrome瀏覽器調(diào)試Android App詳解
這篇文章主要介紹了使用Chrome瀏覽器調(diào)試Android App詳解,本網(wǎng)講解了使用Facebook開源Stetho實(shí)現(xiàn)在Chrome中調(diào)試Android App中,需要的朋友可以參考下2015-05-05

