Chinaunix首页 | 论坛 | 博客
  • 博客访问: 742385
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Android平台

2015-09-09 18:33:45

OSchina-app源代码学习之pullToRefreshListView
PullToRefreshListView是开源项目Android-PullToRefresh里面的控件,下面简单讲解一下如何使用:
1.下载

2.在android studio下导入到项目中

点击(此处)折叠或打开

  1. // 解压,得到 extras,sample,library(主要)
  2. Android-PullToRefresh-master -> library
  3. // 在你项目的根目录创建一个lib目录
  4. [PATH]
  5. [app]
  6.     [src]
  7.     [res]
  8.     [build]
  9.     build.gradle
  10.     ...
  11. [build]
  12. [gradle]
  13. [lib]
  14.     // 将 library 重名名为 pull
  15.     [pull]
  16.         [src]
  17.         [res]
  18.         [build]
  19.         ...
  20. build.gradle
  21. settings.gradle
  22. ...
  23. // 修改 settings.gradle
  24. include ':app', ':lib:pull'
  25. // 修改 [app]/build.gradle
  26. dependencies {
  27.     // Library
  28.     compile project(':lib:pull')
  29. }
  30. // 创建 [lib]/[pull]/build.gradle
  31. apply plugin: 'android-library'
  32. android {
  33.     compileSdkVersion 17
  34.     buildToolsVersion "21.1.2"
  35.     sourceSets {
  36.     main {
  37.         manifest.srcFile 'AndroidManifest.xml'
  38.         java.srcDirs = ['src']
  39.         resources.srcDirs = ['src']
  40.         aidl.srcDirs = ['aidl']
  41.         renderscript.srcDirs = ['src']
  42.         res.srcDirs = ['res']
  43.         assets.srcDirs = ['assets']
  44.     }
  45.     }
  46. }
3.在项目中使用
布局文件:

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     xmlns:ptr=""
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     android:paddingBottom="@dimen/activity_vertical_margin"
  10.     tools:context="com.mdaxue.kuaipu.refresh.PullToRefreshListViewActivity">

  11.     <com.handmark.pulltorefresh.library.PullToRefreshListView
  12.         android:id="@+id/pull_refresh_list"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="fill_parent"
  15.         android:cacheColorHint="#00000000"
  16.         android:divider="#19000000"
  17.         android:dividerHeight="4dp"
  18.         android:fadingEdge="none"
  19.         android:fastScrollEnabled="false"
  20.         android:footerDividersEnabled="false"
  21.         android:headerDividersEnabled="false"
  22.         android:smoothScrollbar="true"
  23.         />

  24. </RelativeLayout>
Activity代码:

点击(此处)折叠或打开

  1. package com.mdaxue.kuaipu.refresh;

  2. import android.app.Activity;
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.text.format.DateUtils;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;

  12. import com.handmark.pulltorefresh.library.PullToRefreshBase;
  13. import com.handmark.pulltorefresh.library.PullToRefreshListView;
  14. import com.mdaxue.kuaipu.R;
  15. import com.mdaxue.kuaipu.base.BaseActivity;

  16. import java.util.LinkedList;

  17. import butterknife.ButterKnife;
  18. import butterknife.InjectView;

  19. public class PullToRefreshListViewActivity extends Activity{

  20.     private LinkedList<String> mListItems;
  21.     private int mItemCount=10;

  22.     @InjectView(R.id.pull_refresh_list)
  23.     PullToRefreshListView mPullRefreshListView;
  24.     private ArrayAdapter<String> mAdapter;

  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_pull_to_refresh_list_view);
  29.         ButterKnife.inject(this);
  30.         initData();
  31.         initView();
  32.         //设置PullRefreshListView上提加载时的加载提示
  33.         mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
  34.         mPullRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
  35.         mPullRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多...");
  36.         // 设置PullRefreshListView下拉加载时的加载提示
  37.         mPullRefreshListView.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
  38.         mPullRefreshListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新...");
  39.         mPullRefreshListView.getLoadingLayoutProxy(true, false).setReleaseLabel("松开刷新...");

  40.     }

  41.     public void initView() {
  42.         mAdapter = new ArrayAdapter<String>(this,
  43.                 android.R.layout.simple_list_item_1, mListItems);
  44.         mPullRefreshListView.setAdapter(mAdapter);
  45.         mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  46.         mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
  47.             //向下刷新
  48.             @Override
  49.             public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
  50.                 new GetRefreshDataTask().execute();
  51.             }

  52.             //向上加载更多
  53.             @Override
  54.             public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
  55.                 String label = DateUtils.formatDateTime(
  56.                         getApplicationContext(),
  57.                         System.currentTimeMillis(),
  58.                         DateUtils.FORMAT_SHOW_TIME
  59.                                 | DateUtils.FORMAT_SHOW_DATE
  60.                                 | DateUtils.FORMAT_ABBREV_ALL);
  61.                 // 显示最后更新的时间
  62.                 mPullRefreshListView.getLoadingLayoutProxy()
  63.                         .setLastUpdatedLabel(label);

  64.                 // 模拟加载任务
  65.                 new GetMoreDataTask().execute();
  66.             }

  67.         });
  68.     }
  69.     public void initData() {
  70.         // 初始化数据和数据源
  71.         mListItems = new LinkedList<String>();

  72.         for (int i = 1; i < mItemCount+1; i++)
  73.         {
  74.             mListItems.add("" + i);
  75.         }
  76.     }

  77.     private class GetRefreshDataTask extends AsyncTask<Void, Void, String>
  78.     {

  79.         @Override
  80.         protected String doInBackground(Void... params)
  81.         {
  82.             try
  83.             {
  84.                 Thread.sleep(1000);
  85.             } catch (InterruptedException e)
  86.             {
  87.             }
  88.             return "" + mItemCount;
  89.         }

  90.         @Override
  91.         protected void onPostExecute(String result)
  92.         {
  93.             mListItems.clear();
  94.             int count = mListItems.size();
  95.             for (int i = 1; i < Integer.parseInt(result)+1; i++)
  96.             {
  97.                 mListItems.add("" + (count+ i));
  98.             }
  99.             mAdapter.notifyDataSetChanged();
  100.             // Call onRefreshComplete when the list has been refreshed.
  101.             mPullRefreshListView.onRefreshComplete();
  102.         }
  103.     }

  104.     private class GetMoreDataTask extends AsyncTask<Void, Void, String>
  105.     {

  106.         @Override
  107.         protected String doInBackground(Void... params)
  108.         {
  109.             try
  110.             {
  111.                 Thread.sleep(1000);
  112.             } catch (InterruptedException e)
  113.             {
  114.             }
  115.             return "" + 10;
  116.         }

  117.         @Override
  118.         protected void onPostExecute(String result)
  119.         {
  120.             int count = mListItems.size();
  121.             for (int i = 1; i < Integer.parseInt(result)+1; i++)
  122.             {
  123.                 mListItems.add("" + (count+ i));
  124.             }
  125.             // Call onRefreshComplete when the list has been refreshed.
  126.             mPullRefreshListView.onRefreshComplete();
  127.         }
  128.     }
  129. }
4.属性简介
模式:
我们添加了一个属性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。 

可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)

当然了,如果你不喜欢在布局文件中指定,完全可以使用代码设置,在onCreate里面写:mPullRefreshListView.setMode(Mode.BOTH);//设置你需要的模式

设置显示文字:

点击(此处)折叠或打开

  1. //设置PullRefreshListView上提加载时的加载提示 mListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
  2.         mListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
  3.         mListView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多..."); // 设置PullRefreshListView下拉加载时的加载提示 mListView.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
  4.         mListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新...");
  5.         mListView.getLoadingLayoutProxy(true, false).setReleaseLabel("松开刷新...");
其它属性:

ptr:ptrDrawable=“” 上拉下拉图标
ptr:ptrAnimationStyle=""  图标动画  取值: flip:翻转 rotate旋转
ptr:ptrHeaderBackground=""  上拉下拉时 头部的背景色
ptr:ptrHeaderTextColor=""     上拉下拉时 文字颜色

还有一些常用属性

ptrRefreshableViewBackground 设置整个mPullRefreshListView的背景色

 

ptrScrollingWhileRefreshingEnabled刷新的时候,是否允许ListView或GridView滚动。觉得为true比较好





阅读(441) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~