OSchina-app源代码学习之pullToRefreshListView
PullToRefreshListView是开源项目Android-PullToRefresh里面的控件,下面简单讲解一下如何使用:
1.下载
2.在android studio下导入到项目中
-
// 解压,得到 extras,sample,library(主要)
-
Android-PullToRefresh-master -> library
-
// 在你项目的根目录创建一个lib目录
-
[PATH]
-
[app]
-
[src]
-
[res]
-
[build]
-
build.gradle
-
...
-
[build]
-
[gradle]
-
[lib]
-
// 将 library 重名名为 pull
-
[pull]
-
[src]
-
[res]
-
[build]
-
...
-
build.gradle
-
settings.gradle
-
...
-
// 修改 settings.gradle
-
include ':app', ':lib:pull'
-
// 修改 [app]/build.gradle
-
dependencies {
-
// Library
-
compile project(':lib:pull')
-
}
-
// 创建 [lib]/[pull]/build.gradle
-
apply plugin: 'android-library'
-
android {
-
compileSdkVersion 17
-
buildToolsVersion "21.1.2"
-
sourceSets {
-
main {
-
manifest.srcFile 'AndroidManifest.xml'
-
java.srcDirs = ['src']
-
resources.srcDirs = ['src']
-
aidl.srcDirs = ['aidl']
-
renderscript.srcDirs = ['src']
-
res.srcDirs = ['res']
-
assets.srcDirs = ['assets']
-
}
-
}
-
}
-
3.在项目中使用
布局文件:
-
<RelativeLayout xmlns:android=""
-
xmlns:tools=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
xmlns:ptr=""
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
android:paddingBottom="@dimen/activity_vertical_margin"
-
tools:context="com.mdaxue.kuaipu.refresh.PullToRefreshListViewActivity">
-
-
<com.handmark.pulltorefresh.library.PullToRefreshListView
-
android:id="@+id/pull_refresh_list"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:cacheColorHint="#00000000"
-
android:divider="#19000000"
-
android:dividerHeight="4dp"
-
android:fadingEdge="none"
-
android:fastScrollEnabled="false"
-
android:footerDividersEnabled="false"
-
android:headerDividersEnabled="false"
-
android:smoothScrollbar="true"
-
/>
-
-
</RelativeLayout>
Activity代码:
-
package com.mdaxue.kuaipu.refresh;
-
-
import android.app.Activity;
-
import android.os.AsyncTask;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.text.format.DateUtils;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
import android.view.View;
-
import android.widget.ArrayAdapter;
-
import android.widget.ListView;
-
-
import com.handmark.pulltorefresh.library.PullToRefreshBase;
-
import com.handmark.pulltorefresh.library.PullToRefreshListView;
-
import com.mdaxue.kuaipu.R;
-
import com.mdaxue.kuaipu.base.BaseActivity;
-
-
import java.util.LinkedList;
-
-
import butterknife.ButterKnife;
-
import butterknife.InjectView;
-
-
public class PullToRefreshListViewActivity extends Activity{
-
-
private LinkedList<String> mListItems;
-
private int mItemCount=10;
-
-
@InjectView(R.id.pull_refresh_list)
-
PullToRefreshListView mPullRefreshListView;
-
private ArrayAdapter<String> mAdapter;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_pull_to_refresh_list_view);
-
ButterKnife.inject(this);
-
initData();
-
initView();
-
//设置PullRefreshListView上提加载时的加载提示
-
mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
-
mPullRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
-
mPullRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多...");
-
// 设置PullRefreshListView下拉加载时的加载提示
-
mPullRefreshListView.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
-
mPullRefreshListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新...");
-
mPullRefreshListView.getLoadingLayoutProxy(true, false).setReleaseLabel("松开刷新...");
-
-
}
-
-
public void initView() {
-
mAdapter = new ArrayAdapter<String>(this,
-
android.R.layout.simple_list_item_1, mListItems);
-
mPullRefreshListView.setAdapter(mAdapter);
-
mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
-
mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
-
//向下刷新
-
@Override
-
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
-
new GetRefreshDataTask().execute();
-
}
-
-
//向上加载更多
-
@Override
-
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
-
String label = DateUtils.formatDateTime(
-
getApplicationContext(),
-
System.currentTimeMillis(),
-
DateUtils.FORMAT_SHOW_TIME
-
| DateUtils.FORMAT_SHOW_DATE
-
| DateUtils.FORMAT_ABBREV_ALL);
-
// 显示最后更新的时间
-
mPullRefreshListView.getLoadingLayoutProxy()
-
.setLastUpdatedLabel(label);
-
-
// 模拟加载任务
-
new GetMoreDataTask().execute();
-
}
-
-
});
-
}
-
public void initData() {
-
// 初始化数据和数据源
-
mListItems = new LinkedList<String>();
-
-
for (int i = 1; i < mItemCount+1; i++)
-
{
-
mListItems.add("" + i);
-
}
-
}
-
-
private class GetRefreshDataTask extends AsyncTask<Void, Void, String>
-
{
-
-
@Override
-
protected String doInBackground(Void... params)
-
{
-
try
-
{
-
Thread.sleep(1000);
-
} catch (InterruptedException e)
-
{
-
}
-
return "" + mItemCount;
-
}
-
-
@Override
-
protected void onPostExecute(String result)
-
{
-
mListItems.clear();
-
int count = mListItems.size();
-
for (int i = 1; i < Integer.parseInt(result)+1; i++)
-
{
-
mListItems.add("" + (count+ i));
-
}
-
mAdapter.notifyDataSetChanged();
-
// Call onRefreshComplete when the list has been refreshed.
-
mPullRefreshListView.onRefreshComplete();
-
}
-
}
-
-
private class GetMoreDataTask extends AsyncTask<Void, Void, String>
-
{
-
-
@Override
-
protected String doInBackground(Void... params)
-
{
-
try
-
{
-
Thread.sleep(1000);
-
} catch (InterruptedException e)
-
{
-
}
-
return "" + 10;
-
}
-
-
@Override
-
protected void onPostExecute(String result)
-
{
-
int count = mListItems.size();
-
for (int i = 1; i < Integer.parseInt(result)+1; i++)
-
{
-
mListItems.add("" + (count+ i));
-
}
-
// Call onRefreshComplete when the list has been refreshed.
-
mPullRefreshListView.onRefreshComplete();
-
}
-
}
-
}
4.属性简介
模式:
我们添加了一个属性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。
可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)
当然了,如果你不喜欢在布局文件中指定,完全可以使用代码设置,在onCreate里面写:mPullRefreshListView.setMode(Mode.BOTH);//设置你需要的模式
设置显示文字:
-
//设置PullRefreshListView上提加载时的加载提示 mListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
-
mListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
-
mListView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多..."); // 设置PullRefreshListView下拉加载时的加载提示 mListView.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
-
mListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新...");
-
mListView.getLoadingLayoutProxy(true, false).setReleaseLabel("松开刷新...");
其它属性:
ptr:ptrDrawable=“” 上拉下拉图标
ptr:ptrAnimationStyle="" 图标动画 取值: flip:翻转 rotate旋转
ptr:ptrHeaderBackground="" 上拉下拉时 头部的背景色
ptr:ptrHeaderTextColor="" 上拉下拉时 文字颜色
还有一些常用属性
ptrRefreshableViewBackground 设置整个mPullRefreshListView的背景色
ptrScrollingWhileRefreshingEnabled刷新的时候,是否允许ListView或GridView滚动。觉得为true比较好
阅读(441) | 评论(0) | 转发(0) |