之前一篇文章已经介绍了万能刷新库的时候,那个库是基于BGARefreshLayout 这个库进行的,后来通过这篇文章,发现廖百万的刷新库也能牛逼,可扩展性十分高,遗憾的是没有加入bga刷新库的对比,所以取舍还得看自己。
言归正传,开工,以美团为例。
- 添加依赖
compile 'in.srain.cube:ultra-ptr:1.0.11'
compile 'in.srain.cube:clog:1.0.2'
compile 'in.srain.cube:cube-sdk:1.0.44.39-SNAPSHOT@aar'
- 布局文件
<in.srain.cube.views.ptr.PtrFrameLayout
android:id="@+id/store_house_ptr_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
cube_ptr:ptr_duration_to_close="200"
cube_ptr:ptr_duration_to_close_header="1000"
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1"
cube_ptr:ptr_resistance="2.0">
<任意view>
</in.srain.cube.views.ptr.PtrFrameLayout>
- 定义刷新头
对不起,要开大咯。
public class MeiTuanHeader extends FrameLayout implements PtrUIHandler {
ImageView headerImg;
private PtrTensionIndicator mPtrTensionIndicator;
PtrFrameLayout mPtrFrameLayout;
View view;
public MeiTuanHeader(Context context) {
super(context);
init(context);
}
public MeiTuanHeader(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MeiTuanHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
view = LayoutInflater.from(context).inflate(R.layout.meituanheader, this, false);
addView(view);
headerImg = (ImageView) view.findViewById(R.id.header_img);
}
public void setUp(PtrFrameLayout ptrFrameLayout) {
mPtrFrameLayout = ptrFrameLayout;
mPtrTensionIndicator = new PtrTensionIndicator();
mPtrFrameLayout.setPtrIndicator(mPtrTensionIndicator);
}
@Override
public void onUIReset(PtrFrameLayout frame) {
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
AnimationDrawable animationDrawable;
headerImg.setImageResource(R.drawable.change);
animationDrawable= (AnimationDrawable) headerImg.getDrawable();
animationDrawable.start();
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
headerImg.clearAnimation();
headerImg.setImageResource(R.drawable.bga_refresh_mt_pull_down);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@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 ) {
//未到达刷新线
if (status == PtrFrameLayout.PTR_STATUS_PREPARE) {
headerImg.setImageResource(R.drawable.bga_refresh_mt_pull_down);
headerImg.setScaleX((float) currentPos / mOffsetToRefresh);
headerImg.setScaleY((float) currentPos / mOffsetToRefresh);
}
} else if (currentPos > mOffsetToRefresh ) {
//到达或超过刷新线
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
AnimationDrawable animationDrawable;
headerImg.setImageResource(R.drawable.fanshen);
animationDrawable= (AnimationDrawable) headerImg.getDrawable();
animationDrawable.start();
}
}
}
}
- Activity中添加头,监听
private void MeiTuanRefresh() {
MeiTuanHeader header = new MeiTuanHeader(MeiTuanActivity.this);
storeHousePtrFrame.setHeaderView(header);
storeHousePtrFrame.addPtrUIHandler(header);
storeHousePtrFrame.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) {
storeHousePtrFrame.postDelayed(new Runnable() {
@Override
public void run() {
storeHousePtrFrame.refreshComplete();
}
}, 1800);
}
});
}
ok,步骤差不多完成,上效果图。(算了,效果图跟之前差不多,传送门)
刷新功能已经完成,可在其基础上任意的拓展,遗憾的是没有加载更多,原作者说了,加载更多是任意view的事情,跟我木有关系,好吧,那咱就只能针对listview,gridview还有recyclerview自己封装写footer了,具体demo略。