亲自动手编写Android通用刷新控件
程序员文章站
2023-11-28 22:40:52
项目中我们经常有上拉、下拉刷新的需求,几乎所有的listview、recyclerview都会伴随着上拉、下拉刷新的需求,如果我们使用一些开源控件,换了控件我们就要更新,现...
项目中我们经常有上拉、下拉刷新的需求,几乎所有的listview、recyclerview都会伴随着上拉、下拉刷新的需求,如果我们使用一些开源控件,换了控件我们就要更新,现在我们自己撸起袖子写一个通用的刷新控件
项目地址:
思路:
- 写一个继承relativelayout的refreshlayout
- 添加头尾控件作为刷新控件
- 通过事件分发来进行刷新操作
- 通过动画来控制控件移动
目的:让他的所有子控件都可以使用,哪怕是一个textview
public class refreshlayout extends relativelayout { /** * 滑动控件时拉去的速度比例 */ private final int v_refresh = 2; /** * 是否是刷新过程 * true 是 * false 不是 * 为false的时候才可以进行刷新 */ private boolean misrefreshduring; /** * 可以进下拉刷新 */ private boolean mcandownpull; /** * 可以进行上拉刷新 */ private boolean mcanuppull; /** * 判断触摸后是否是初次移动 */ private boolean misfirstmove; /** * y轴呢平移的距离 */ private int mdistancey; /** * 刷新接口对象 */ private onrefresh monrefresh; /** * 用于控制事件拦截的变量 */ private boolean mcanintercept; private int mtouchslop; private int mdistance; private layoutparams mheaderparams; private view mheaderview; private view mfootview; private int mheadermaxheight; private int mstarty; private layoutparams mfootparams; private int mfootmaxheight; private pullcallback mcallback; private view mchildview; private objectanimator manimator; public refreshlayout(context context) { super(context); initdata(); } public refreshlayout(context context, attributeset attrs) { super(context, attrs); initdata(); } public refreshlayout(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); initdata(); } /** * 必须让头尾控件实现的接口 */ public interface headandfootcallback { //设置属性 void setattribute(); //开始刷新 void startpull(); //停止刷新 void stoppull(); } /** * 必须让被拖动的控件子类实现 */ public interface pullcallback { boolean candownpull(); boolean canuppull(); } private void initdata() { //不调用该方法不能进行绘制 setwillnotdraw(false); } /** * 下拉刷新完成后必须使用该方法 */ public void downpullfinish() { manimator.setfloatvalues(mchildview.gettranslationy(), 0); manimator.start(); ((headandfootcallback) mheaderview).stoppull(); } /** * 上拉完成后必须调用该方法 */ public void uppullfinish() { manimator.setfloatvalues(mchildview.gettranslationy(), 0); manimator.start(); ((headandfootcallback) mfootview).stoppull(); } /** * 自动下拉刷新 */ public void autodownpullforhead() { postdelayed(new runnable() { @override public void run() { mcandownpull = true; mcanuppull = false; manimator.setfloatvalues(10, mheadermaxheight); manimator.start(); ((headandfootcallback) mheaderview).startpull(); monrefresh.ondownpullrefresh(); } }, 500); } /** * 自动下拉刷新 */ public void autouppullforhead() { postdelayed(new runnable() { @override public void run() { mcandownpull = false; mcanuppull = true; manimator.setfloatvalues(0, mfootmaxheight); manimator.start(); ((headandfootcallback) mfootview).startpull(); monrefresh.onuppullrefresh(); } }, 500); } @override public boolean onintercepttouchevent(motionevent ev) { return mcanintercept; } @override public boolean ontouchevent(motionevent event) { return true; } @override public boolean dispatchtouchevent(motionevent event) { log.e("shen", "misrefreshduring=" + misrefreshduring); if (misrefreshduring) /**如果正在进行刷新将不会获取motionevent*/ { return super.dispatchtouchevent(event); } switch (event.getaction()) { case motionevent.action_down: mstarty = (int) event.gety(); initpull(); break; case motionevent.action_move: if (event.getpointercount() == 1) { int movey = (int) event.gety(); mdistancey = (movey - mstarty) / v_refresh; if (!misfirstmove && mdistancey != 0 && mdistancey < mtouchslop) { mcandownpull = mdistancey > 0; mcanuppull = !mcandownpull; misfirstmove = true; } if (mcandownpull && mcallback.candownpull()) { updatafordownpull();//下拉刷新 mchildview.setenabled(false); mcanintercept = true; } if (mcanuppull && mcallback.canuppull()) { updataforuppull();//上拉加载 mchildview.setenabled(false); mcanintercept = true; } mstarty = movey; } break; case motionevent.action_up: misrefreshduring = true; misfirstmove = false; if (mheaderparams.height >= mheadermaxheight) /**可以下拉刷新**/ { ((headandfootcallback) mheaderview).startpull(); monrefresh.ondownpullrefresh(); } else if (mfootparams.height >= mfootmaxheight) /**可以上拉刷新**/ { ((headandfootcallback) mfootview).startpull(); monrefresh.onuppullrefresh(); } else if (mheaderparams.height > 0 && mheaderparams.height < mheadermaxheight) /**不能进行下拉刷新,收回**/ { releasefordownfinished(); } else if (mfootparams.height > 0 && mfootparams.height < mfootmaxheight) /**不能进行下拉刷新,收回**/ { releaseforupfinished(); } else { misrefreshduring = false; mcanintercept = false; } break; } super.dispatchtouchevent(event); return true; } /** * 每次进行触摸都需要进行初始化 */ private void initpull() { mcandownpull = false; mcanuppull = false; } /** * 不需要进行上拉刷新 */ private void releaseforupfinished() { manimator.setfloatvalues(mchildview.gettranslationy(), 0); manimator.start(); } /** * 不需要进行下拉刷新 */ private void releasefordownfinished() { manimator.setfloatvalues(mchildview.gettranslationy(), 0); manimator.start(); } /** * 上拉时处理手势 */ private void updataforuppull() { if (mdistancey != 0) { mfootparams.height -= mdistancey; if (mfootparams.height <= 0) { mfootparams.height = 0; } if (mfootparams.height >= mfootmaxheight) { mfootparams.height = mfootmaxheight; } mchildview.settranslationy(-mfootparams.height); mfootview.requestlayout(); } } /** * 下拉时处理手势 */ private void updatafordownpull() { if (mdistancey != 0) { mheaderparams.height += mdistancey; if (mheaderparams.height >= mheadermaxheight) { //最大 mheaderparams.height = mheadermaxheight; } if (mheaderparams.height <= 0) { //最小 mheaderparams.height = 0; } mchildview.settranslationy(mheaderparams.height); mheaderview.requestlayout(); } } @override protected void onattachedtowindow() { super.onattachedtowindow(); } @override protected void onfinishinflate() { super.onfinishinflate(); //加载头 mheaderview = getchildat(0); if (!(mheaderview instanceof headandfootcallback)) { new illegalstateexception("headerview必须实现headandfootcallback接口"); } ((headandfootcallback) mheaderview).setattribute(); mheaderparams = (layoutparams) mheaderview.getlayoutparams(); mheaderparams.addrule(relativelayout.align_parent_top); //加载尾 mfootview = getchildat(2); if (!(mfootview instanceof headandfootcallback)) { new illegalstateexception("footview必须实现headandfootcallback接口"); } ((headandfootcallback) mfootview).setattribute(); mfootparams = (layoutparams) mfootview.getlayoutparams(); mfootparams.addrule(relativelayout.align_parent_bottom); mchildview = getchildat(1); if (!(mchildview instanceof headandfootcallback)) { new illegalstateexception("childview必须实现pullcallback接口"); } mcallback = (pullcallback) getchildat(1); //设置动画 manimator = objectanimator.offloat(mchildview, "translationy", 0); manimator.setinterpolator(new decelerateinterpolator()); manimator.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator animation) { int translationy = (int) mchildview.gettranslationy(); if (mcanuppull) { //从移动到的位置往下滑 mfootparams.height = math.abs(translationy); mfootview.requestlayout(); } else if (mcandownpull) { mheaderparams.height = math.abs(translationy); mheaderview.requestlayout(); } log.e("shen", "translationy=" + translationy); log.e("shen", "mheaderparams.height=" + mheaderparams.height); if (translationy == 0) { mchildview.setenabled(true); mdistancey = 0; //重置 misrefreshduring = false; //重置 mcanintercept = false; } else { misrefreshduring = true; } } }); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); mtouchslop = viewconfiguration.get(getcontext()).getscaledtouchslop(); mdistance = mtouchslop * 5; //设置下拉头初始属性 mheadermaxheight = mheaderparams.height; mheaderparams.height = 0; mheaderview.requestlayout(); //设置上拉尾初始属性 mfootmaxheight = mfootparams.height; mfootparams.height = 0; mfootview.requestlayout(); } /** * 下拉/上拉事件监听 */ public interface onrefresh { /** * 下拉刷新 */ void ondownpullrefresh(); /** * 上拉加载 */ void onuppullrefresh(); } public void setonrefresh(onrefresh onrefresh) { monrefresh = onrefresh; } }
给他添加三个控件,头尾就是刷新头、尾,第二个就是正常显示的控件。必须让头尾实现headandfootcallback接口,来设置属性,通知开始刷新、结束刷新
难点: 现在来说下开发时遇到的难点
- 由于判断在dispatchtouchevent中,导致如果该控件以及子控件都不消费该事件的话,就会造成事件不会发送到它,因为如果不消费down事件的话,之后所有的事件都不会在进行接收。解决方式,让该控件ontouchevent方法消返回true,当子控件不进行事件消费的话,就会返回由该控件消费,不会造成因down事件不消费而无法接收到事件,导致dispatchtouchevent也不消费事件
- 动画,动画就是我的伤痛,最近在学习估值器
这个控件自认为写的不错,通过他可以帮我们学习事件分发、动画、接口回调,也是有一定的学习意义
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 世界上最危险十大猫科动物 猎豹仅排第十,西伯利亚虎最凶猛
下一篇: 百度口碑下线个人网站的口碑评价!