PullToRefresh

来源:互联网 发布:简约时尚的淘宝店铺名 编辑:程序博客网 时间:2024/05/16 05:13
PullToRefresh是一套实现非常好的下拉刷新库,它支持:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
等多种常用的需要刷新的View类型,而且使用起来也十分方便。
(下载地址:https://github.com/chrisbanes/Android-PullToRefresh

PullToRefresh基本用法:
1、在布局文件中添加PullToRefresh控件,比如PullToRefreshListView;
2、在Activity中,设置监听器OnRefreshListener以响应用户下拉操作;
3、在监听器的onRefresh()方法中执行数据刷新操作,可以通过AsyncTask来实现;
4、在AsyncTask中获取到数据后,记得调用onRefreshComplete()方法通知PullToRefresh控件数据已获取完毕,可以结束刷新操作。

实例:PullToRefreshDemo
运行效果:

视图结构



代码清单:
布局文件:activity_main.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.    
  11.     <com.handmark.pulltorefresh.library.PullToRefreshListView  
  12.         android:id="@+id/pull_to_refresh_listview"  
  13.         android:layout_height="fill_parent"  
  14.         android:layout_width="fill_parent" />  
  15.   
  16. </RelativeLayout>  

Java源代码文件:MainActivity.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.rainsong.pulltorefreshdemo;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.LinkedList;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.AsyncTask;  
  8. import android.os.Bundle;  
  9. import android.text.format.DateUtils;  
  10. import android.view.Menu;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.ListView;  
  13.   
  14. import com.handmark.pulltorefresh.library.PullToRefreshBase;  
  15. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;  
  16. import com.handmark.pulltorefresh.library.PullToRefreshListView;  
  17.   
  18. public class MainActivity extends Activity {  
  19.     private PullToRefreshListView mPullToRefreshListView;  
  20.     private LinkedList<String> mListItems;  
  21.     private ArrayAdapter<String> mAdapter;  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         // Set a listener to be invoked when the list should be refreshed.  
  29.         mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);  
  30.         mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {  
  31.             @Override  
  32.             public void onRefresh(PullToRefreshBase<ListView> refreshView) {  
  33.                 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
  34.                         DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
  35.                   
  36.                 // Update the LastUpdatedLabel  
  37.                 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  
  38.                   
  39.                 // Do work to refresh the list here.  
  40.                 new GetDataTask().execute();  
  41.             }  
  42.         });  
  43.           
  44.         ListView actualListView = mPullToRefreshListView.getRefreshableView();  
  45.           
  46.         mListItems = new LinkedList<String>();  
  47.         mListItems.addAll(Arrays.asList(mStrings));  
  48.           
  49.         mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);  
  50.         actualListView.setAdapter(mAdapter);  
  51.     }  
  52.       
  53.     private class GetDataTask extends AsyncTask<Void, Void, String[]> {  
  54.   
  55.         @Override  
  56.         protected String[] doInBackground(Void... params) {  
  57.             // Simulates a background job.  
  58.             try {  
  59.                 Thread.sleep(4000);  
  60.             } catch (InterruptedException e) {  
  61.             }  
  62.             return mStrings;  
  63.         }  
  64.         @Override  
  65.         protected void onPostExecute(String[] result) {  
  66.             mListItems.addFirst("Added after refresh...");  
  67.             mAdapter.notifyDataSetChanged();  
  68.               
  69.             // Call onRefreshComplete when the list has been refreshed.  
  70.             mPullToRefreshListView.onRefreshComplete();  
  71.             super.onPostExecute(result);  
  72.         }  
  73.     }  
  74.   
  75.     @Override  
  76.     public boolean onCreateOptionsMenu(Menu menu) {  
  77.         // Inflate the menu; this adds items to the action bar if it is present.  
  78.         getMenuInflater().inflate(R.menu.main, menu);  
  79.         return true;  
  80.     }  
  81.   
  82.     private String[] mStrings = { "John""Michelle""Amy""Kim""Mary",  
  83.             "David""Sunny""James""Maria""Michael""Sarah""Robert",  
  84.             "Lily""William""Jessica""Paul""Crystal""Peter",  
  85.             "Jennifer""George""Rachel""Thomas""Lisa""Daniel""Elizabeth",  
  86.             "Kevin" };  
  87. }  

实例源代码下载 http://download.csdn.net/detail/hantangsongming/8336075



转自:http://blog.csdn.net/hantangsongming/article/details/42490277

0 0
原创粉丝点击