直接上代码
下拉刷新涉及到的类
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
public class MyRefreshHeader extends LinearLayout implements RefreshHeader {
private ImageView mImage;
private AnimationDrawable mAnimPull;
private AnimationDrawable mAnimRefresh;
/**
* 1,构造方法
*/
public MyRefreshHeader(Context context) {
this(context, null, 0);
}
public MyRefreshHeader(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.m_refresh_header, this);
mImage = view.findViewById(R.id.iv_refresh_header);
}
/**
* 2,获取真实视图(必须返回,不能为null)一般就是返回当前自定义的view
*/
@NonNull
@Override
public View getView() {
return this;
}
/**
* 3,获取变换方式(必须指定一个:平移、拉伸、固定、全屏),Translate指平移,大多数都是平移
*/
@NonNull
@Override
public SpinnerStyle getSpinnerStyle() {
return SpinnerStyle.Translate;
}
/**
* 4,执行下拉的过程
*
* @param isDragging
* @param percent
* @param offset
* @param height
* @param maxDragHeight
*/
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
if (percent < 1) {
mImage.setScaleX(percent);
mImage.setScaleY(percent);
}
}
/**
* 5,一般可以理解为一下case中的三种状态,在达到相应状态时候开始改变
* 注意:这三种状态都是初始化的状态
*/
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
switch (newState) {
//1,下拉刷新的开始状态:下拉可以刷新
case PullDownToRefresh:
mImage.setImageResource(R.drawable.commonui_pull_image);
break;
//2,下拉到最底部的状态:释放立即刷新
case ReleaseToRefresh:
mImage.setImageResource(R.drawable.anim_pull_end);
mAnimPull = (AnimationDrawable) mImage.getDrawable();
mAnimPull.start();
break;
//3,下拉到最底部后松手的状态:正在刷新
case Refreshing:
mImage.setImageResource(R.drawable.anim_pull_refreshing);
mAnimRefresh = (AnimationDrawable) mImage.getDrawable();
mAnimRefresh.start();
break;
}
}
/**
* 6,结束下拉刷新的时候需要关闭动画
*
* @param refreshLayout
* @param success
* @return
*/
@Override
public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
if (mAnimRefresh != null && mAnimRefresh.isRunning()) {
mAnimRefresh.stop();
}
if (mAnimPull != null && mAnimPull.isRunning()) {
mAnimPull.stop();
}
return 0;
}
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void setPrimaryColors(int... colors) {
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override
public boolean isSupportHorizontalDrag() {
return false;
}
}
m_refresh_header.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/white"
android:padding="5dp"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_refresh_header"
android:scaleX="0"
android:scaleY="0"
android:translationY="0dp"
android:layout_width="41dp"
android:layout_height="54dp" />
</LinearLayout>
drawable/anim_pull_refreshing.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_01" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_02" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_03" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_02" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_05" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_06" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_07" />
<item android:duration="50" android:drawable="@drawable/commonui_refreshing_image_frame_06" />
</animation-list>
/drawable/anim_pull_end.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:drawable="@drawable/commonui_pull_end_image_frame_01"
android:duration="100" />
<item
android:drawable="@drawable/commonui_pull_end_image_frame_02"
android:duration="100" />
<item
android:drawable="@drawable/commonui_pull_end_image_frame_03"
android:duration="100" />
<item
android:drawable="@drawable/commonui_pull_end_image_frame_04"
android:duration="100" />
<item
android:drawable="@drawable/commonui_pull_end_image_frame_05"
android:duration="100" />
</animation-list>
上拉加载:
public class MyRefreshFooter extends LinearLayout implements RefreshFooter {
private ImageView mImage;
private Animation mAnim;
public MyRefreshFooter(Context context) {
this(context, null, 0);
}
public MyRefreshFooter(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRefreshFooter(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.m_refresh_footer, this);
mImage = view.findViewById(R.id.iv_refresh_footer);
mAnim = AnimationUtils.loadAnimation(getContext(), R.anim.anim_round_rotate);
LinearInterpolator linearInterpolator = new LinearInterpolator();
mAnim.setInterpolator(linearInterpolator);
}
@Override
public boolean setNoMoreData(boolean noMoreData) {
return false;
}
@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) {
//控制是否稍微上滑动就刷新
kernel.getRefreshLayout().setEnableAutoLoadMore(false);
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
}
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
if(mAnim != null && mAnim.hasStarted() && !mAnim.hasEnded()){
mAnim.cancel();
mImage.clearAnimation();
}
return 0;
}
@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 (newState) {
case None:
case PullUpToLoad:
if (mAnim != null) {
mImage.startAnimation(mAnim);
}
break;
case Loading:
case LoadReleased:
break;
case ReleaseToLoad:
break;
}
}
}
自定义的footer文件对应的xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/iv_refresh_footer"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/default_ptr_rotate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="让你放心 才是对的"
android:textSize="11sp" />
</LinearLayout>
简单用法:
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.smart)
SmartRefreshLayout smart;
@BindView(R.id.text)
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
private void initView() {
smartRefreshView();
}
/**
* 1,刷新控件的监听
*/
private void smartRefreshView() {
smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
@Override
public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
//延迟3秒关闭
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.finishLoadMore();
}
}, 3000);
}
@Override
public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
//2,刷新完成关闭,正常情况是请求接口完成关闭
//3,如果需要在网络请求结束后关闭,则调用
refreshLayout.finishRefresh();
refreshLayout.setNoMoreData(true);
}
});
}
@OnClick(R.id.text)
public void onViewClicked() {
smart.finishRefresh();
}
}
MainActivity对应xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<xxx.SmartRefreshLayout
android:id="@+id/smart"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlEnablePreviewInEditMode="true"
app:srlPrimaryColor="#00000000">
<com.mumu.jsrecyclerview5.MRefreshHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5500ff00"
android:gravity="center">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我爱安卓"
android:textColor="#ff00ff"
android:textSize="30sp" />
</LinearLayout>
<xxx.MRefreshFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
另外一种使用方法:
MyRefreshHeader myRefreshHeader = new MyRefreshHeader(context)
myRefreshHeader.setBackgroundColor(Res.getColor(android.R.color.white))
refresh_layout.setRefreshHeader(myRefreshHeader)
此方法不需要在xml中配置
<com.mumu.jsrecyclerview5.MRefreshHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<xxx.MRefreshFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
完!