超简单的 listview的第三方控件PullToRefreshListView 上拉刷新,下拉加载更多,分页加载
ptr是pullToRefresh的配置属性
使用是需要添加 xmlns:ptr="http://schemas.android.com/apk/res-auto"
ptr:ptrDrawable=“” 上下拉图标
ptr:ptrAnimationStyle="" 图标动画 flip:翻转 rotate旋转
ptr:ptrHeaderBackground="" 上下拉 头部的背景色
ptr:ptrHeaderTextColor="" 上下拉 文字颜色
ptrRefreshableViewBackground 设置整个mPullRefreshListView的背景色
android:layout_height="match_parent" 一般设置成match_parent 可以包裹在布局里,设置成wrap_content有些情况就显示不出来了,看看源码你就一清二楚
Mode.BOTH:同时支持上下拉
Mode.PULL_FROM_START:只支持下拉
Mode.PULL_FROM_END:只支持上拉
xml
ptr:ptrMode="both"
可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)
下拉上拉 图标和文字 位置改动是在PullToRefresh源代码中改的即:PullToRefreshListView.handleStyledAttributes 中lp的Gravity改为CENTER_VERTICAL
如果想要改动图标和文字的距离和布局 在这library项目下这两个文件改
pull_to_refresh_header_horizontal.xml
pull_to_refresh_header_vertical.xml
显然在实际操作的时候也会用到其他监听
setOnScrollListener()
SCROLL_STATE_TOUCH_SCROLL 正在滚动
SCROLL_STATE_FLING 手指做了抛的动作(手指离开屏幕前,用力滑了一下)
SCROLL_STATE_IDLE 停止滚动
setOnLastItemVisibleListener
当用户拉到底时调用
setOnItemClickListener()
每一个item设置事件position-1
setLoadingDrawable(null) 里可以设置下和上拉图片,null设置不要图片
xml布局
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/law_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#ffffff"
android:dividerHeight="5dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:listSelector="#00000000"
android:smoothScrollbar="true"
ptr:ptrHeaderBackground="#c80202"
ptr:ptrHeaderTextColor="#F39801" />
activity代码
package com.zcycjy.mobile.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zcycjy.mobile.R;
import com.zcycjy.mobile.adapter.LawAdapter;
import com.zcycjy.mobile.adapter.LawSpAdapter;
import com.zcycjy.mobile.app.BaseActivity;
import com.zcycjy.mobile.biz.BizData;
import com.zcycjy.mobile.entity.Law;
import com.zcycjy.mobile.entity.Law.LawListEntity;
public class LawActivity extends Activity {
private List<LawListEntity> lawList = null;
private PullToRefreshListView law_lv;
private LawAdapter adapter;
private int nextPage = 1;// 页
private ImageView iv_loading;
private final static int TYPE_200 = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_law);
initView();
setEventListener();
}
final Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (TYPE_200 == msg.what) {
Law law = (Law) msg.obj;
if (law != null) {
String result = law.getResult();
if (("Y".equals(result)) && (null != law.getLawList())
&& (0 != law.getLawList().size())) {
if (1 == nextPage) {
lawList.clear();
}
lawList.addAll(law.getLawList());
adapter.refreshDatas(lawList);
}
String page = law.getPage();
if ("0".equals(page)) {
// 刷新
law_lv.onRefreshComplete();
law_lv.setMode(Mode.PULL_FROM_START);
} else {
// 刷新、下一页
law_lv.onRefreshComplete();
law_lv.setMode(Mode.BOTH);
}
}
dismissLoading(iv_loading);// 根据自己的要求,加载完关闭加载进度
}
};
};
private void bizData(int type) {
switch (type) {
case TYPE_200:
showLoading(iv_loading);// 根据自己的要求,加载开始显示加载进度
BizData.lawData(handler, TYPE_200,nextPage);// 里面写你要加载的数据
break;
default:
break;
}
}
/**
* 设置监听
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setEventListener() {
law_lv.setOnRefreshListener(new OnRefreshListener2() {
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
// 下拉刷新触发的事件
// 获取格式化的时间
String label = DateUtils.formatDateTime(
getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// 更新LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
nextPage = 1;
bizData(TYPE_200);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
nextPage += 1;
bizData(TYPE_200);
}
});
}
private void initView() {
iv_loading = (ImageView) findViewById(R.id.iv_loading);// 加载进度
lawList = new ArrayList<LawListEntity>();
law_lv = (PullToRefreshListView) findViewById(R.id.law_lv);
law_lv.setMode(Mode.BOTH);
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);// 你想要得图片
law_lv.getLoadingLayoutProxy(false, true).setLoadingDrawable(d);
// 上拉加载更多,分页加载
law_lv.getLoadingLayoutProxy(false, true).setPullLabel("加载更多");
law_lv.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载中...");
law_lv.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载");
// 下拉刷新
law_lv.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新");
law_lv.getLoadingLayoutProxy(true, false).setRefreshingLabel("更新中...");
law_lv.getLoadingLayoutProxy(true, false).setReleaseLabel("松开更新");
adapter = new LawAdapter(this, lawList);
law_lv.setAdapter(adapter);
}
}
日常中差不多够用了,也可以根据自己需求去扩展,也可以自己写一个,大体逻辑一样