Android深入探究自定义View之嵌套滑动的实现
本文主要探讨以下几个问题:
- 嵌套滑动设计目的
- 嵌套滑动的实现
- 嵌套滑动与事件分发机制
嵌套滑动设计目的
不知道大家有没有注意过淘宝app首页的二级联动,滑动的商品的时候上面类别也会滑动,滑动过程中类别模块停了商品还能继续滑动。也就是说滑动的是view,viewgroup也会跟着滑动。如果用事件分发机制处理也能处理,但会及其麻烦。那用nestedscroll会咋样?
嵌套滑动的实现
假设布局如下
recyclerview 实现了 nestedscrollingchild 接口,nestedscrollview 实现了 nestedscrollingparent,这是实现嵌套布局的基础
public class recyclerview extends viewgroup implements scrollingview, nestedscrollingchild2, nestedscrollingchild3 public class nestedscrollview extends framelayout implements nestedscrollingparent3, nestedscrollingchild3, scrollingview
滑动屏幕时 recyclerview 收到滑动事件,在 action_down 时
// recyclerview.java ontouchevent函数 case motionevent.action_down: { mscrollpointerid = e.getpointerid(0); minitialtouchx = mlasttouchx = (int) (e.getx() + 0.5f); minitialtouchy = mlasttouchy = (int) (e.gety() + 0.5f); int nestedscrollaxis = viewcompat.scroll_axis_none; if (canscrollhorizontally) { nestedscrollaxis |= viewcompat.scroll_axis_horizontal; } if (canscrollvertically) { nestedscrollaxis |= viewcompat.scroll_axis_vertical; } // startnestedscroll(nestedscrollaxis, type_touch); } break;
继续深入
public boolean startnestedscroll(@scrollaxis int axes, @nestedscrolltype int type) { if (hasnestedscrollingparent(type)) { // already in progress return true; } if (isnestedscrollingenabled()) { viewparent p = mview.getparent(); view child = mview; while (p != null) { if (viewparentcompat.onstartnestedscroll(p, child, mview, axes, type)) { setnestedscrollingparentfortype(type, p); viewparentcompat.onnestedscrollaccepted(p, child, mview, axes, type); return true; } if (p instanceof view) { child = (view) p; } p = p.getparent(); } } return false; }
递归寻找nestedscrollingparent,然后回调 onstartnestedscroll 和 onnestedscrollaccepted 。onstartnestedscroll 决定了当前控件是否能接收到其内部view(非并非是直接子view)滑动时的参数;按下时确定其嵌套的父布局以及是否能收到后续事件。再看action_move事件
case motionevent.action_move: { if (dispatchnestedprescroll(dx, dy, mscrollconsumed, mscrolloffset)) { dx -= mscrollconsumed[0]; dy -= mscrollconsumed[1]; vtev.offsetlocation(mscrolloffset[0], mscrolloffset[1]); } } break;
action_move 中调用了 dispatchnestedprescroll 。dispatchnestedprescroll 中会回调 onnestedprescroll 方法,内部的 scrollbyinternal 中还会回调 onnestedscroll 方法
整个流程如下
onnestedprescroll中,我们判断,如果是上滑且顶部控件未完全隐藏,则消耗掉dy,即consumed[1]=dy;如果是下滑且内部view已经无法继续下拉,则消耗掉dy,即consumed[1]=dy,消耗掉的意思,就是自己去执行scrollby,实际上就是我们的nestedscrollview 滑动。
public void onnestedprescroll(@nonnull view target, int dx, int dy, @nonnull int[] consumed, int type) { // 向上滑动。若当前topview可见,需要将topview滑动至不可见 boolean hidetop = dy > 0 && getscrolly() < topview.getmeasuredheight(); if (hidetop) { scrollby(0, dy); // 这个是被消费的距离,如果没有会被重复消费现象是父布局与子布局同时滑动,滑动的距离被消费两次 consumed[1] = dy; } }
整体代码如下
public class nestedscrolllayout extends nestedscrollview { private view topview; private viewgroup contentview; private static final string tag = "nestedscrolllayout"; public nestedscrolllayout(context context) { this(context, null); init(); } public nestedscrolllayout(context context, @nullable attributeset attrs) { this(context, attrs, 0); init(); } public nestedscrolllayout(context context, @nullable attributeset attrs, int defstyleattr) { this(context, attrs, defstyleattr, 0); init(); } public nestedscrolllayout(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr); init(); } private flinghelper mflinghelper; int totaldy = 0; /** * 用于判断recyclerview是否在fling */ boolean isstartfling = false; /** * 记录当前滑动的y轴加速度 */ private int velocityy = 0; private void init() { mflinghelper = new flinghelper(getcontext()); setonscrollchangelistener(new view.onscrollchangelistener() { @override public void onscrollchange(view v, int scrollx, int scrolly, int oldscrollx, int oldscrolly) { if (isstartfling) { totaldy = 0; isstartfling = false; } if (scrolly == 0) { log.e(tag, "top scroll"); // refreshlayout.setenabled(true); } if (scrolly == (getchildat(0).getmeasuredheight() - v.getmeasuredheight())) { log.e(tag, "bottom scroll"); dispatchchildfling(); } //在recyclerview fling情况下,记录当前recyclerview在y轴的偏移 totaldy += scrolly - oldscrolly; } }); } private void dispatchchildfling() { if (velocityy != 0) { double splineflingdistance = mflinghelper.getsplineflingdistance(velocityy); if (splineflingdistance > totaldy) { childfling(mflinghelper.getvelocitybydistance(splineflingdistance - double.valueof(totaldy))); } } totaldy = 0; velocityy = 0; } private void childfling(int vely) { recyclerview childrecyclerview = getchildrecyclerview(contentview); if (childrecyclerview != null) { childrecyclerview.fling(0, vely); } } @override public void fling(int velocityy) { super.fling(velocityy); if (velocityy <= 0) { this.velocityy = 0; } else { isstartfling = true; this.velocityy = velocityy; } } @override protected void onfinishinflate() { super.onfinishinflate(); topview = ((viewgroup) getchildat(0)).getchildat(0); contentview = (viewgroup) ((viewgroup) getchildat(0)).getchildat(1); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // 调整contentview的高度为父容器高度,使之填充布局,避免父容器滚动后出现空白 super.onmeasure(widthmeasurespec, heightmeasurespec); viewgroup.layoutparams lp = contentview.getlayoutparams(); lp.height = getmeasuredheight(); contentview.setlayoutparams(lp); } /** * 解决滑动冲突:recyclerview在滑动之前会问下父布局是否需要拦截,父布局使用此方法 */ @override public void onnestedprescroll(@nonnull view target, int dx, int dy, @nonnull int[] consumed, int type) { log.e("nestedscrolllayout", getscrolly()+"::onnestedprescroll::"+topview.getmeasuredheight()+"::dy::"+dy); // 向上滑动。若当前topview可见,需要将topview滑动至不可见 boolean hidetop = dy > 0 && getscrolly() < topview.getmeasuredheight(); if (hidetop) { scrollby(0, dy); // 这个是被消费的距离,如果没有会被重复消费,现象是父布局与子布局同时滑动 consumed[1] = dy; } } private recyclerview getchildrecyclerview(viewgroup viewgroup) { for (int i = 0; i < viewgroup.getchildcount(); i++) { view view = viewgroup.getchildat(i); if (view instanceof recyclerview && view.getclass() == nestedlogrecyclerview.class) { return (recyclerview) viewgroup.getchildat(i); } else if (viewgroup.getchildat(i) instanceof viewgroup) { viewgroup childrecyclerview = getchildrecyclerview((viewgroup) viewgroup.getchildat(i)); if (childrecyclerview instanceof recyclerview) { return (recyclerview) childrecyclerview; } } continue; } return null; } }
嵌套滑动与事件分发机制
- 事件分发机制:子view首先得到事件处理权,处理过程中父view可以对其拦截,但是拦截了以后就无法再还给子view(本次手势内)。
- nestedscrolling 滑动机制:内部view在滚动的时候,首先将dx,dy交给nestedscrollingparent,nestedscrollingparent可对其进行部分消耗,剩余的部分还给内部view。
总结:嵌套布局要注意的有几个方面
- action_down 时子view调用父布局的onstartnestedscroll,根据滑动方向判断父布局是否要收到子view的滑动参数
- action_move时子view调用父布局的onnestedprescroll函数,父布局是否要滑动已经消费掉自身需要的距离
- action_up时,手指抬起可能还有加速度,调用父布局的onprefling判断是否需要消费以及消费剩下的再传给子布局
到此这篇关于android深入探究自定义view之嵌套滑动的实现的文章就介绍到这了,更多相关android 嵌套滑动内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 如何做一个文本搜索?