Android SwipeRefreshLayout 官方下拉重新整理控制元件介紹
阿新 • • 發佈:2018-12-19
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/24521483
下面App基本都有下拉重新整理的功能,以前基本都使用XListView或者自己寫一個下拉重新整理,近期Google提供了一個官方的下拉重新整理控制元件SwipeRefreshLayout,我感覺還不錯啊,見慣了傳統的下拉重新整理,這個反而給人耳目一新的感覺(貌似知乎的APP已經使用這種下拉重新整理了)。
Google也在官方網站給出了V4的相容包:
順便看一眼API唄:
和XlistView差不多,還是很方便使用的,大概就這4個常用的方法,下面貼個簡單的例子。
1、佈局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/id_listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout></RelativeLayout>
2、MainActivty:package com.zhy.swiperefreshlayoutdemo;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.util.Log;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener{ private static final int REFRESH_COMPLETE = 0X110; private SwipeRefreshLayout mSwipeLayout; private ListView mListView; private ArrayAdapter<String> mAdapter; private List<String> mDatas = new ArrayList<String>(Arrays.asList("Java", "Javascript", "C++", "Ruby", "Json", "HTML")); private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case REFRESH_COMPLETE: mDatas.addAll(Arrays.asList("Lucene", "Canvas", "Bitmap")); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); break; } }; }; @SuppressLint("InlinedApi") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.id_listview); mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly); mSwipeLayout.setOnRefreshListener(this); mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); } public void onRefresh() { // Log.e("xxx", Thread.currentThread().getName()); // UI Thread mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); }}
好了,結束,我感覺效果挺好看的,不妨試試。效果圖:
ok ~