最近用到了android-Ultra-Pull-To-Refresh这个库,感觉功能如实的强大。
此库的地址是:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh 想了解的可以去github上看,介绍还是很详细的。
我在这里只简单介绍一下使用此库自定义头部刷新布局。
1.首先在Module:app中加入
implementation 'in.srain.cube:ultra-ptr:1.0.11'
2. 在你想刷新的布局中加入PtrFrameLayout,包裹你要刷新布局。
例如:
<in.srain.cube.views.ptr.PtrFrameLayout
android:id="@+id/store_house_ptr_frame"
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
cube_ptr:ptr_resistance="1.7"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_duration_to_close="300"
cube_ptr:ptr_duration_to_close_header="2000"
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false" >
<LinearLayout
android:id="@+id/store_house_ptr_image_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cube_mints_333333"
android:clickable="true"
android:padding="10dp">
<in.srain.cube.image.CubeImageView
android:id="@+id/store_house_ptr_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</in.srain.cube.views.ptr.PtrFrameLayout>
3. 现在开始自定义头部布局,定义一个类集成view(LinearLayout,FrameLayout,等)实现 PtrUIHandler
package com.muxi.ant.ui.view;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.muxi.ant.R;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
//首页下拉刷新的头部
//参考:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/blob/master/README-cn.md
//https://blog.csdn.net/shineflowers/article/details/59033536
public class JdRefreshHeader extends FrameLayout implements PtrUIHandler {
private TextView mTvRemind;
private int mState;
private ImageView iv;
private ProgressBar progress;
private AnimationDrawable frameAnim;
/**
* 重置
* 准备刷新
* 开始刷新
* 结束刷新
*/
public static final int STATE_RESET = -1;
public static final int STATE_PREPARE = 0;
public static final int STATE_BEGIN = 1;
public static final int STATE_FINISH = 2;
public JdRefreshHeader(Context context) {
this(context, null);
}
public JdRefreshHeader(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public JdRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化view
*/
private void initView() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_header_view, this, false);
mTvRemind = (TextView) view.findViewById(R.id.tv_remain);
iv = view.findViewById(R.id.iv);
progress = view.findViewById(R.id.progress);
// 通过逐帧动画的资源文件获得AnimationDrawable示例
frameAnim = (AnimationDrawable) getResources().getDrawable(R.drawable.fist_loading);
// 把AnimationDrawable设置为ImageView的背景
iv.setBackgroundDrawable(frameAnim);
addView(view);
}
@Override
public void onUIReset(PtrFrameLayout frame) {
//重置
mState = STATE_RESET;
mTvRemind.setText(R.string.dropdown_refresh);
// llLinearlayout.setVisibility(VISIBLE);
progress.setVisibility(GONE);
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
//准备刷新
mState = STATE_PREPARE;
//llLinearlayout.setVisibility(GONE);
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
//开始刷新 显示刷新进度跟文本
mState = STATE_BEGIN;
progress.setVisibility(VISIBLE);
start();
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
//刷新完成 设置文本 设置进度隐藏
mState = STATE_FINISH;
//llLinearlayout.setVisibility(VISIBLE);
progress.setVisibility(GONE);
stop();
}
@Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
switch (mState) {
case STATE_PREPARE:
if (ptrIndicator.getCurrentPercent() < 0.4) {
mTvRemind.setText(R.string.dropdown_refresh);
} else {
mTvRemind.setText(R.string.loosen_the_refresh);
}
break;
case STATE_BEGIN:
mTvRemind.setText(R.string.Refreshing);
break;
case STATE_FINISH:
mTvRemind.setText(R.string.load_completion);
break;
}
}
/**
* 开始播放
*/
protected void start() {
if (frameAnim != null && !frameAnim.isRunning()) {
frameAnim.start();
}
}
/**
* 停止播放
*/
protected void stop() {
if (frameAnim != null && frameAnim.isRunning()) {
frameAnim.stop();
}
}
}
这里的 R.layout.refresh_header_view 布局,就可以根据自己项目的需求进行修改。
4)使用自定义的下拉刷新头部
final JdRefreshHeader header = new JdRefreshHeader(getContext());
// header.setPadding(0, PtrLocalDisplay.dp2px(200), 0, 0);
ptrframelayout.setHeaderView(header); //设置刷新头部
ptrframelayout.addPtrUIHandler(header);
//处理左右滑动冲突
ptrframelayout.disableWhenHorizontalMove(true);
ptrframelayout.setPtrHandler(new PtrHandler() {
@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return isrefresh;
//return false;
}
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
//开始刷新--todo
}
});
5)以下是 android-Ultra-Pull-To-Refresh参数简介 :
app:ptr_resistance="1.7":设置下拉的阻尼系数,值越大感觉越难下拉
ptr_ratio_of_header_height_to_refresh:设置超过头部的多少时,释放可以执行刷新操作
ptr_duration_to_close:设置下拉回弹的时间
ptr_duration_to_close_header:设置刷新完成,头部回弹时间,注意和前一个进行区别
ptr_keep_header_when_refresh:设置刷新的时候是否保持头部
ptr_pull_to_fresh:设置下拉过程中执行刷新,我们一般设置为false
参考文档:
https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/blob/master/README-cn.md
https://blog.csdn.net/shineflowers/article/details/59033536