Android SwipeRefreshLayout下拉刷新組件示例
SwipeRefreshLayout概述
SwipeRefrshLayout是Google官方更新的一個(gè)Widget,可以實(shí)現(xiàn)下拉刷新的效果。該控件集成自ViewGroup在support-v4兼容包下,不過我們需要升級(jí)supportlibrary的版本到19.1以上。
用戶通過手勢或者點(diǎn)擊某個(gè)按鈕實(shí)現(xiàn)內(nèi)容視圖的刷新,布局里加入SwipeRefreshLayout嵌套一個(gè)子視圖如ListView、 RecyclerView等,觸發(fā)刷新會(huì)通過OnRefreshListener的onRefresh方法回調(diào),我們在這里執(zhí)行頁面數(shù)據(jù)的刷新,每次手勢 的完成都會(huì)執(zhí)行一次通知,根據(jù)滑動(dòng)距離判斷是否需要回調(diào)。setRefreshing(false)通過代碼直接取消刷新,true則手動(dòng)設(shè)置刷新調(diào)出刷 新視圖。setEnabled(false)通過boolean控制是否禁用手勢刷新 。
基本使用的方法如下:
1.setOnRefreshListener(OnRefreshListener):添加下拉刷新監(jiān)聽器
2.setRefreshing(boolean):顯示或者隱藏刷新進(jìn)度條
3.isRefreshing():檢查是否處于刷新狀態(tài)
使用非常簡單,用一個(gè)簡單案例來介紹SwipeRefreshLayout下拉刷新的功能。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/v7_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back">
<android.support.v7.widget.RecyclerView
android:id="@+id/v7_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
app:cardCornerRadius="5dp"
app:cardBackgroundColor="@android:color/white"
android:layout_margin="5dp"
android:layout_height="60dp"
android:layout_width="match_parent">
<TextView
android:id="@+id/menuitem_tv"
android:layout_gravity="center"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
Activity
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private List<String> list=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initView();
}
private void initView()
{
swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.v7_refresh);
recyclerView=(RecyclerView)findViewById(R.id.v7_recyclerView);
//設(shè)置下拉圓圈的大小,兩個(gè)值 LARGE, DEFAULT
swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
// 設(shè)定下拉圓圈的背景:默認(rèn)white
// swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
initData();
}
private void initData()
{
bindData();
//設(shè)置刷新時(shí)動(dòng)畫的顏色,可以設(shè)置4個(gè)
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText (MainActivity.this,"正在刷新",Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText (MainActivity.this,"刷新完成",Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
}
}, 4000);
}
});
}
private void bindData(){
list=new ArrayList<>();
for(int i=0;i<22;i++){
list.add("Item"+(i+1));
}
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
MenuAdapter menuAdapter=new MenuAdapter(this,R.layout.item,list);
recyclerView.setAdapter(menuAdapter);
menuAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
Toast.makeText (MainActivity.this, list.get(position),Toast.LENGTH_LONG).show();
}
});
}
}
運(yùn)行效果如圖:

源碼點(diǎn)擊下載:SwipeRefreshLayout_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
- Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除
- Android SwipereFreshLayout下拉刷新
- Android實(shí)現(xiàn)SwipeRefreshLayout首次進(jìn)入自動(dòng)刷新
- Android SwipeRefreshLayout下拉刷新源碼解析
- Android SwipeRefreshLayout超詳細(xì)講解
相關(guān)文章
Android編程實(shí)現(xiàn)自定義title功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義title功能,結(jié)合具體實(shí)例形式分析了Android自定義title的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android源碼學(xué)習(xí)之單例模式應(yīng)用及優(yōu)點(diǎn)介紹
動(dòng)態(tài)確保某一個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例這就是Android單例模式應(yīng)用,接下來詳細(xì)介紹,有需求的朋友可以參考下2013-01-01
Android 官推 kotlin-first 的圖片加載庫——Coil的使用入門
這篇文章主要介紹了Android 官推 kotlin-first 的圖片加載庫——Coil的使用入門,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Flutter開發(fā)之Shortcuts快捷鍵組件的用法詳解
在桌面端的開發(fā)中,鍵盤快捷鍵是非常常見而必要的,F(xiàn)lutter?既然可以開發(fā)桌面端應(yīng)用,那必然要提供自定義快捷鍵,所以本文就來和大家講講Shortcuts組件的簡單使用吧2023-05-05
Android 圖片保存到相冊不顯示的解決方案(兼容Android 10及更高版本)
這篇文章主要介紹了Android 圖片保存到系統(tǒng)相冊不顯示的解決方案,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動(dòng)畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05

