您的位置:首页 > 其它

PullToRefreshListView的使用

2016-05-08 21:54 316 查看
PullToRefreshListView是Android-PullToRefresh开源项目中的一个自定义组件,Android-PullToRefresh是一个强大的拉动刷新开源项目,支持各种控件下拉刷新,ListView、ViewPager、WebView、ExpandableListView、GridView、ScrollView、Horizontal ScrollView、Fragment 上下左右拉动刷新。

今天就讲解PullToRefreshListView的使用,其他的组件会在接下来的博客中讲解。

开源地址:https://github.com/chrisbanes/Android-PullToRefresh

下载源码后,打开项目,如图



将上图的library导入Eclipse中,然后关联你的 项目,如果不会关联的话,可以到这里去看看/article/7626679.html

好了,一切工作准备好了。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
>
<!-- 上述的xmlns:ptr="http://schemas.android.com/apk/res-auto"是必须的,下面的属性才可以用 -->
<!--   替代ListView -->

<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="#ffffff"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrHeaderTextColor="#ffffff"
/>

</LinearLayout>


xmlns:ptr=”http://schemas.android.com/apk/res-auto”是必须的,只有这样ptr:ptrHeaderTextColor才可以用,还有其他属性,可以在library中的attrs.xml中找到,其他属性没有什么解释的。如图



MainActivity文件代码,先全部贴出。

package com.zengfeng.pulltorefreshlistviewdemo;

import java.util.Arrays;
import java.util.LinkedList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

//数据
private LinkedList<String> mListItems;
//更新布局listView
private PullToRefreshListView mPullRefreshListView;
//适配器
private ArrayAdapter<String> madapter;
//数据
private String[] mListItem={"Android","Java","Phthy","C#","PHP"};

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//初始化mPullRefreshListView
mPullRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list);

//设置刷新更新监听方式
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

@Override
//跟新方法
public void onRefresh(PullToRefreshBase<ListView> refreshView) {

//设置下拉时的时间显示
String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
//更新label显示
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

//进行异步更新
new GetDataTask().execute();
}
});

//设置滑动尾部的监听器
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

@Override
public void onLastItemVisible() {

//滑动到底部是,提示已经到底部了
Toast.makeText(MainActivity.this, "已经滑动到底部了", Toast.LENGTH_LONG).show();

}
});

//刷新时是否允许滑动
//mPullRefreshListView.isScrollingWhileRefreshingEnabled();

//在刷新时允许继续滑动
mPullRefreshListView.setScrollingWhileRefreshingEnabled(true);

//得到模式,有三种,分别是Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END
//mPullRefreshListView.getMode();

//设置上下都可以刷新
mPullRefreshListView.setMode(Mode.BOTH);

//得到刷新的listView
ListView actualListView=mPullRefreshListView.getRefreshableView();

mListItems=new LinkedList<>();
mListItems.addAll(Arrays.asList(mListItem));
madapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mListItems);
//设置适配器,下面两个效果相同
//mPullRefreshListView.setAdapter(madapter);
actualListView.setAdapter(madapter);
}

private class GetDataTask extends AsyncTask<Void, Void, Void>{

@Override
//后台执行任务
protected Void doInBackground(Void... params) {

try {
Thread.sleep(1000);
} catch (Exception e) {

}
return null;
}

@Override
protected void onPostExecute(Void result) {

//得到刷新当前模式
Mode mode=mPullRefreshListView.getCurrentMode();
if(mode==Mode.PULL_FROM_START){
//如果模式是开头刷新效果
mListItems.addFirst("START Add");
}else{
//如果模式是尾部刷新效果
mListItems.addLast("Later Add");
}

//通知数据已经改变了
madapter.notifyDataSetChanged();
//加载完以后停止刷新
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}

}

}


上述代码已经注释了,应该看懂是不难的。

测试效果图:

刷新前







好了,PullToRefreshListView的使用学会了,有什么疑问可以留言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: