首先给出这个强大的下拉刷新库的地址 https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh
效果图:
最新版版本号: 1.0.11, 发布到了: https://oss.sonatype.org/content/repositories/snapshots
在gradle中:
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
稳定版: 1.0.11, https://oss.sonatype.org/content/repositories/releases,
在gradle中:
mavenCentral()
gradle / Android Studio, 最新版:
compile 'in.srain.cube:ultra-ptr:1.0.11'
gradle / Android Studio, 稳定版:
compile 'in.srain.cube:ultra-ptr:1.0.11'
做好准备工作后就可以开始定制自己的下拉刷新头部了。
"http://schemas.android.com/apk/res/android"
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/baseBg"
android:fitsSystemWindows="true"
android:orientation="vertical">
<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_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_resistance="2.0">
.support.v7.widget.RecyclerView
android:id="@+id/re_fill_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/baseBg"/>
in.srain.cube.views.ptr.PtrFrameLayout>
PtrFrameLayout是下拉刷新控件,作为父容器。其中的属性可以在github上找到对应的意义。
下面的RecyclerView 可以换成任意内容。
首先要定义头部的布局
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/baseBg">
"@+id/tv_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="loading...."
android:textColor="@color/colorTextLightBlack" />
<com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/spin_kit"
style="@style/SpinKitView.Circle"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="32dp"
android:visibility="gone"
app:SpinKit_Color="@color/colorTextLightBlack" />
我这里是一个TextView + github上的圆形进度。可以替换成自己喜欢的布局。
自定义头部代码:
//继承FrameLayout 实现库的接口
public class LoadingHeader extends FrameLayout implements PtrUIHandler {
private TextView tvLoading;
private TextView tvLoaded;
private PtrTensionIndicator mPtrTensionIndicator;
private SpinKitView spinKitView;
PtrFrameLayout mPtrFrameLayout;
private View view;
public LoadingHeader(Context context) {
this(context, null);
}
public LoadingHeader(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadingHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
//实例化控件
private void init() {
view = LayoutInflater.from(getContext()).inflate(R.layout.view_loading_head, this, false);
addView(view);
tvLoading = (TextView) view.findViewById(R.id.tv_loading);
spinKitView = (SpinKitView) view.findViewById(R.id.spin_kit);
}
//重写方法
@Override
public void onUIReset(PtrFrameLayout frame) {
//重置
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
//准备刷新
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
//开始刷新 显示刷新进度跟文本
tvLoading.setText("刷新中");
spinKitView.setVisibility(VISIBLE);
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
//刷新完成 设置文本 设置进度隐藏
tvLoading.setText("刷新完成");
spinKitView.setVisibility(GONE);
}
@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) {
tvLoading.setText("下拉刷新");
}
} else if (currentPos > mOffsetToRefresh) {
//到达或超过刷新线
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
tvLoading.setText("释放刷新");
}
}
}
//
public void setHeadColor(int color){
view.setBackgroundColor(color);
}
}
使用:
//实例化自定义头部
LoadingHeader header = new LoadingHeader(ManagedLoanListActivity.this);
//刷新时保留头部
storeHousePtrFrame.setKeepHeaderWhenRefresh(true);
//设置刷新头部
storeHousePtrFrame.setHeaderView(header);
storeHousePtrFrame.addPtrUIHandler(header);
//处理左右滑动冲突
storeHousePtrFrame.disableWhenHorizontalMove(true);
//进入页面自动刷新必须放到postDelayed()里 不然无效
storeHousePtrFrame.postDelayed(new Runnable() {
@Override
public void run() {
storeHousePtrFrame.autoRefresh();
}
}, 100);
//设置刷新事件
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) {
//开始刷新 获取服务器数据
getData(ACTION_REFRESH);
}
});
//刷新完成调用这个 storeHousePtrFrame.refreshComplete();
如果你想你的界面都有滑动阻尼效果的话,这个库也可以这样用:
添加一个空布局的头部:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="40dp" />
LinearLayout>
加载空的头部
View view = getLayoutInflater().inflate(R.layout.view_head, null);
storeHousePtrFrame.setHeaderView(view);
storeHousePtrFrame.setKeepHeaderWhenRefresh(false);
storeHousePtrFrame.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
//开始刷新就设置他刷新完成,头部上弹
storeHousePtrFrame.refreshComplete();
}
});
这样以后就不用到处找刷新的库啦。