這篇文章給大家介紹Android項目中使用SwipeRefreshLayout組件實現一個下拉刷新功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
SwipeRefreshLayout概述
SwipeRefrshLayout是Google官方更新的一個Widget,可以實現下拉刷新的效果。該控件集成自ViewGroup在support-v4兼容包下,不過我們需要升級supportlibrary的版本到19.1以上。
用戶通過手勢或者點擊某個按鈕實現內容視圖的刷新,布局里加入SwipeRefreshLayout嵌套一個子視圖如ListView、 RecyclerView等,觸發刷新會通過OnRefreshListener的onRefresh方法回調,我們在這里執行頁面數據的刷新,每次手勢 的完成都會執行一次通知,根據滑動距離判斷是否需要回調。setRefreshing(false)通過代碼直接取消刷新,true則手動設置刷新調出刷 新視圖。setEnabled(false)通過boolean控制是否禁用手勢刷新 。
基本使用的方法如下:
1.setOnRefreshListener(OnRefreshListener):添加下拉刷新監聽器
2.setRefreshing(boolean):顯示或者隱藏刷新進度條
3.isRefreshing():檢查是否處于刷新狀態
使用非常簡單,用一個簡單案例來介紹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);
//設置下拉圓圈的大小,兩個值 LARGE, DEFAULT
swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
// 設定下拉圓圈的背景:默認white
// swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
initData();
}
private void initData()
{
bindData();
//設置刷新時動畫的顏色,可以設置4個
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();
}
});
}
}關于Android項目中使用SwipeRefreshLayout組件實現一個下拉刷新功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。