一直用的下拉刷新库就是android-Ultra-Pull-to-Refresh,本身这个库就带有几种样式的下拉刷新头部,大家可以去git看一下,地址https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh。最为方便的是我们可以自己定制各式各样的头部。最近项目有个自定义的下拉头部,自己研究了一下。实现效果是:
开发之自定义下拉刷新头部-安卓自定义下拉刷新">
我们要做的就是自己写一个样式xml文件,然后实现PtrUIHandler这个接口,代码如下:
public class LoadMoreFooterView extends FrameLayout implements PtrUIHandler {
private LayoutInflater inflater;
// 下拉刷新视图(头部视图)
private ViewGroup headView;
// 下拉刷新文字
private TextView tvHeadTitle;
// 下拉图标
private ImageView ivWindmill;
// private WindmillDrawable drawable;
public LoadMoreFooterView(Context context) {
this(context, null);
}
public LoadMoreFooterView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
/**
* 初始化
*
* @param context
*/
private void init(Context context) {
inflater = LayoutInflater.from(context);
/**
* 头部
*/
headView = (ViewGroup) inflater.inflate(R.layout.widget_header, this, true);
ivWindmill = (ImageView) headView.findViewById(R.id.iv_windmill);
tvHeadTitle = (TextView) headView.findViewById(R.id.tv_head_title);
ivWindmill.setVisibility(VISIBLE);
ivWindmill.setImageResource(R.mipmap.icon_logo);
tvHeadTitle.setText("下拉刷新");
}
@Override
public void onUIReset(PtrFrameLayout ptrFrameLayout) {
tvHeadTitle.setText("下拉刷新");
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
tvHeadTitle.setText("下拉刷新");
}
@Override
public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
tvHeadTitle.setText("正在刷新");
}
@Override
public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
ivWindmill.clearAnimation();
tvHeadTitle.setText("刷新完成");
}
@Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
final int mOffsetToRefresh = frame.getOffsetToRefresh();
final int currentPos = ptrIndicator.getCurrentPosY();
final int lastPos = ptrIndicator.getLastPosY();
if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
tvHeadTitle.setText("下拉刷新");
}
} else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
tvHeadTitle.setText("松开刷新");
}
}
}
}
使用:
/* 创建自定义刷新头部view */
LoadMoreFooterView header = new LoadMoreFooterView(this);
/* 设置刷新头部view */
ptr_view.setHeaderView(header);
/* 设置回调 */
ptr_view.addPtrUIHandler(header);
ptr_view.setPtrHandler(new PtrHandler() {
@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
}
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
ptr_view.postDelayed(new Runnable() {
@Override
public void run() {
getdata();
}
}, 2000);
}
});
/* 延时100秒,自动刷新 */
ptr_view.postDelayed(new Runnable() {
@Override
public void run() {
ptr_view.autoRefresh();
}
}, 100);