在我们使用SmartRefreshLayout的时候也就是它 有默认的上拉刷新下滑 加载更多的图标或文字 所有写了一下自定义头部刷新图标和底部刷新 。
思路:
添加刷新底部和头部依赖
创建头部和底部的view 都 继承(extends) ConstraintLayout 实现(implements) RefreshHeader
创建XML 布局
在Application里面构造全局(避免在每个页面添加Head,Foot 的Xml布局)
在我们刷新的时候有时会下拉但是不动。如何处理?
答:我使用了帧动画也就是将gif 转为png 图片,每次下拉的时候不会自己转动。具体在自定义View中体现
1. 添加刷新底部和头部依赖
implementation deps.other.smartRefreshLayout
implementation deps.other.smartRefreshHeader
2. 创建头部和底部的view 都 继承(extends) ConstraintLayout 实现(implements) RefreshHeader
a.Head:
public class SmartRefreshHeader extends ConstraintLayout implements RefreshHeader {
private GifView mGifView;
private ImageView mMaskView;
private DecimalFormat decimalFormat = new DecimalFormat("000");
public SmartRefreshHeader(Context context) {
super(context);
}
public SmartRefreshHeader(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SmartRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mGifView = findViewById(R.id.gifView);
mMaskView = findViewById(R.id.maskView);
mGifView.setVisibility(INVISIBLE);
mGifView.pause();
}
@NonNull
@Override
public View getView() {
return this;
}
@NonNull
@Override
public SpinnerStyle getSpinnerStyle() {
return SpinnerStyle.Translate;
}
@Override
public void setPrimaryColors(int... colors) {
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
mGifView.setVisibility( offset > 1 ? INVISIBLE : VISIBLE);
mMaskView.setVisibility(offset > 1 ? VISIBLE : INVISIBLE);
if (isDragging && percent <= 1) {
int p = (int) (percent * 100) / 2; // (0 ~ 50)
String prefixName = "header" + decimalFormat.format(p);
int id = getResources().getIdentifier(prefixName, "drawable", getContext().getPackageName());
if (id > 0) {
mMaskView.setBackgroundResource(id);
}
}
}
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
mMaskView.setVisibility(INVISIBLE);
mGifView.setVisibility(VISIBLE);
mGifView.play();
}
@Override
public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
mGifView.pause();
return 500;
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override
public boolean isSupportHorizontalDrag() {
return false;
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
//switch ()
}
}
b.Foot
public class SmartRefreshFooter extends ConstraintLayout implements RefreshFooter {
private GifView mGifView;
private ImageView mMaskView;
private DecimalFormat decimalFormat = new DecimalFormat("000");
public SmartRefreshFooter(Context context) {
super(context);
}
public SmartRefreshFooter(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SmartRefreshFooter(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mGifView = findViewById(R.id.gifView);
mMaskView = findViewById(R.id.maskView);
mGifView.setVisibility(INVISIBLE);
mGifView.pause();
}
@NonNull
@Override
public View getView() {
return this;
}
@NonNull
@Override
public SpinnerStyle getSpinnerStyle() {
return SpinnerStyle.Translate;
}
@Override
public void setPrimaryColors(int... colors) {
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
if (isDragging && percent <= 1) {
if(!mGifView.isPlaying()){
mGifView.setVisibility(INVISIBLE);
mMaskView.setVisibility(VISIBLE);
int p = (int) (percent * 100) / 2; // (0 ~ 50)
String prefixName = "header" + decimalFormat.format(p);
int id = getResources().getIdentifier(prefixName, "drawable", getContext().getPackageName());
if (id > 0) {
mMaskView.setBackgroundResource(id);
}
}else{
mMaskView.setVisibility(INVISIBLE);
}
}
}
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
mMaskView.setVisibility(INVISIBLE);
mGifView.setVisibility(VISIBLE);
mGifView.play();
}
@Override
public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
mGifView.pause();
return 500; // 加载完成后多久关闭,即回弹会去
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override
public boolean isSupportHorizontalDrag() {
return false;
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
}
@Override
public boolean setNoMoreData(boolean noMoreData) {
return false;
}
}
创建自定义VIew 的xml
a.header:
<com.wds.see_road.widgets.SmartRefreshHeader
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
>
<com.cunoraz.gifview.library.GifView
android:id="@+id/gifView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:gif="@drawable/mvp_loading"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/maskView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</com.wds.see_road.widgets.SmartRefreshHeader>
b.footer:
<com.wds.see_road.widgets.SmartRefreshFooter
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content"
>
<com.cunoraz.gifview.library.GifView
android:id="@+id/gifView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:gif="@drawable/mvp_loading"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/maskView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</com.wds.see_road.widgets.SmartRefreshFooter>
4.使用:
public class MyApp extends Application {
public static MyApp app;
public static void setApp(MyApp app) {
MyApp.app = app;
}
@Override
public void onCreate() {
super.onCreate();
app=this;
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@NonNull
@Override
public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
return (RefreshHeader) LayoutInflater.from(context).inflate(R.layout.layout_refresh_head,null,false);
}
});
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
@NonNull
@Override
public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
return (RefreshFooter) LayoutInflater.from(context).inflate(R.layout.layout_refresh_footer,null,false);
}
});
}
}
必须在清单文件声明:android:name=".data.net.service.MyApp"
网友评论