轉自:http://blog.csdn.net/zouzhigang96/article/details/50476402
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
前言:
如今谷歌推出了更官方的下拉刷新控件, 這無疑是對安卓開發人員來說是個好消息,很方便的使用這個SwipeRefreshLayout控件實現下拉刷新功能。Android4.0以下的版本需要用到 Android-support-v4.jar包才能用到
android-support-v4.jar 包下載地址:http://download.csdn.net/detail/h7870181/7784247
注:記得一定要把Support library的版本升級到19.1或最新
那下面就來做一個簡單的DEMO,來看看SwipeRefreshLayout這個控件到底如何
注:樓主使用的是Android Studio 作為IDE。
1,首先來看看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/swiperereshlayout" android:layout_marginTop="55dp" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2,在Activity中是如何使用SwipeRefreshLayout的
public class MainActivity extends AppCompatActivity { private SwipeRefreshLayout swiperereshlayout ; private ListView listview ; private ArrayAdapter<String> adapter; private List<String> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initView(); } private void initView (){ swiperereshlayout = (SwipeRefreshLayout) findViewById(R.id.swiperereshlayout); listview = (ListView) findViewById(R.id.listview); data = new ArrayList<String>(); for (int i = 0; i < 20; i++) { data.add("當前的item為 " + i); } adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data); listview.setAdapter(adapter); swiperereshlayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light); //給swipeRefreshLayout綁定刷新監聽 swiperereshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //設置2秒的時間來執行以下事件 new Handler().postDelayed(new Runnable() { public void run() { data.add(0, "刷新后新增的item"); adapter.notifyDataSetChanged(); swiperereshlayout.setRefreshing(false); } }, 2000); } }); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
注:主要代碼就是在initView這個方法里面,里面簡單的實現的如何下拉加載新數據,只是做了一個簡單的演示 ,具體深入的開發,大家也可以嘗試一下自定義。
主要方法
(1)setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener
(2)setRefreshing(boolean): 顯示或隱藏刷新進度條
(3)isRefreshing(): 檢查是否處於刷新狀態
(4)setColorSchemeResources(): 設置進度條的顏色主題,最多能設置四種
目前 swiperereshlayout.setColorScheme()->已棄用
可以使用swiperereshlayout.setColorSchemeResources()來設置顏色。
總結:
谷歌官方目前正在不斷完善自己的SDK,推出越來越多的組件,其目的是讓開發更簡單,設計上更統一,這可能是Google未來的方向,不管怎樣,這對開發者來說無疑是非常好的消息。