前言
接上一篇博客,Ultra-Pull-To-Refresh超简单终极实现下拉刷新、上拉加载 入门实现(一)
上一篇是实现了简单的自带的刷新和加载功能。但是这样简单的动画往往不能满足我们的需求,所以这一片用一个京东刷新的例子来实现自定义,就可以举一反三地干了,
制作gif太麻烦了,希望不影响各位老师的心情
准备工作
一、图片素材
下载京东APP,解压。找到此文件夹复制即可JDMALL-PC2/r/n
二、刷新的顶部布局
新建jd_refresh_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/layout_tx">
<ImageView
android:id="@+id/iv_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/refresh_people_0" />
<ImageView
android:id="@+id/iv_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:src="@mipmap/refresh_goods_0"
/>
</FrameLayout>
<LinearLayout
android:id="@+id/layout_tx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="让购物更便捷"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_remain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="松开刷新"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
三、动画xml书写
drawable文件夹下建立running.xml文件。就是人跑动的动画
由于京东的图片名字太长,我截取了最后几个单词。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item
android:drawable="@mipmap/refresh_people_1"
android:duration="70" />
<item
android:drawable="@mipmap/refresh_people_2"
android:duration="70" />
<item
android:drawable="@mipmap/refresh_people_3"
android:duration="70" />
</animation-list>
四、最重要的一步
新建刷新的文件,用来控制动画和文字。继承FrameLayout,实现框架的PtrUIHandler接口
public class JdRefreshHeader extends FrameLayout implements PtrUIHandler {
/**
* 提醒文本
*/
private TextView mTvRemind;
/**
* 快递员logo
*/
private ImageView mIvMan;
/**
* 商品logo
*/
private ImageView mIvGoods;
/**
* 状态识别
*/
private int mState;
/**
* 重置
* 准备刷新
* 开始刷新
* 结束刷新
*/
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 static final int MARGIN_RIGHT = 100;
/**
* 动画
*/
private AnimationDrawable mAnimationDrawable;
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.jd_refresh_header, this, false);
mTvRemind = (TextView) view.findViewById(R.id.tv_remain);
mIvMan = (ImageView) view.findViewById(R.id.iv_man);
mIvGoods = (ImageView) view.findViewById(R.id.iv_goods);
addView(view);
}
@Override
public void onUIReset(PtrFrameLayout frame) {
mState = STATE_RESET;
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
mState = STATE_PREPARE;
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
mState = STATE_BEGIN;
//隐藏商品logo,开启跑步动画
mIvGoods.setVisibility(View.GONE);
mIvMan.setBackgroundResource(R.drawable.running);
mAnimationDrawable = (AnimationDrawable) mIvMan.getBackground();
if (!mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame, boolean isHeader) {
mState = STATE_FINISH;
mIvGoods.setVisibility(View.VISIBLE);
//停止动画
if (mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
}
mIvMan.setBackgroundResource(R.mipmap.refresh_people_0);
}
@Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
//处理提醒字体
switch (mState) {
case STATE_PREPARE:
//logo设置
mIvMan.setAlpha(ptrIndicator.getCurrentPercent());
mIvGoods.setAlpha(ptrIndicator.getCurrentPercent());
LayoutParams params = (LayoutParams) mIvMan.getLayoutParams();
if (ptrIndicator.getCurrentPercent() <= 1) {
mIvMan.setScaleX(ptrIndicator.getCurrentPercent());
mIvMan.setScaleY(ptrIndicator.getCurrentPercent());
mIvGoods.setScaleX(ptrIndicator.getCurrentPercent());
mIvGoods.setScaleY(ptrIndicator.getCurrentPercent());
int marginRight = (int) (MARGIN_RIGHT - MARGIN_RIGHT * ptrIndicator.getCurrentPercent());
params.setMargins(0, 0, marginRight, 0);
mIvMan.setLayoutParams(params);
}
if (ptrIndicator.getCurrentPercent() < 1.2) {
mTvRemind.setText("下拉刷新...");
} else {
mTvRemind.setText("松开刷新...");
}
break;
case STATE_BEGIN:
mTvRemind.setText("更新中...");
break;
case STATE_FINISH:
mTvRemind.setText("加载完成...");
break;
}
}
}
五、Activity引入
接上一篇,
JdRefreshHeader mJdRefreshHeader = new JdRefreshHeader(this);
ptrFrameLayout.setHeaderView(mJdRefreshHeader);
ptrFrameLayout.addPtrUIHandler(mJdRefreshHeader);
这三行代码就可以了。
至此,大功告成。代码复制可用,有什么问题,欢迎指出,谢谢。