大佬教程收集整理的这篇文章主要介绍了Android中NestedScrolling滑动机制详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1,如今nestedScrolling运用到很多地方了,要想好看一点的滑动变换,基本上就是使用这个来完成的,让我们来简单的了解一下。
2,nestedScrolling机制能够让父View和子View在滚动式进行配合,其基本流程如下:
而要实现这样的交互机制,首先父view要实现nestedScrollingParent接口,而子View需要实现N恩斯特大S从rollingChild接口,在这套机制中子View是发起者,父view是接受回调并做出响应的。
一下是几个关键的类和接口
//主要接口 nestedScrollingChild nestedScrollingParent //帮助类 nestedScrollingChildHelper nestedScrollingParentHelper
一些新的系统View已经帮我们实现了以上两个接口,也就是说他们是支持nestedScrolling,例如:
nestedScrollView已经实现了nestedScrollingChild和nestedScrollingParent两个接口
RecycleView已经实现了nestedScrollingChild
CoordinatorLayout实现了nestedScrollingParent
nestedScrollingChild接口
//开始、停止嵌套滚动 public Boolean startnestedScroll(int axes); public void stopnestedScroll(); //触摸滚动相关 public Boolean dispatchnestedPreScroll(int dx,int dy,int[] consumed,int[] offseTinWindow); public Boolean dispatchnestedScroll(int dxConsumed,int dyConsumed,int dxUnconsumed,int dyUnconsumed,int[] offseTinWindow); //惯性滚动相关 public Boolean dispatchnestedPreFling(float veLocityX,float veLocityY); public Boolean dispatchnestedFling(float veLocityX,float veLocityY,Boolean consumed);
开启嵌套滚动流程(实际上是进行了一些嵌套滚动前准备工作)。
当找到了能够配合当前子view进行嵌套滚动的父view时,返回值为true(Returns:true if a cooperative parent was found and nested scrolling has been enabled for the current gesture)。
在子view自己进行滚动之前调用此方法,询问父view是否要在子view之前进行滚动。
此方法的前两个参数用于告诉父View此次要滚动的距离;而第三第四个参数用于子view获取父view消费掉的距离和父view位置的偏移量。
第一第二个参数为输入参数,即常规的函数参数,调用函数的时候我们需要为其传递确切的值。而第三第四个参数为输出参数,调用函数时我们只需要传递容器(在这里就是两个数组),在调用结束后,我们就可以从容器中获取函数输出的值。
如果parent消费了一部分或全部距离,则此方法返回true。
在子view自己进行滚动之后调用此方法,询问父view是否还要进行余下(unconsumed)的滚动。
前四个参数为输入参数,用于告诉父view已经消费和尚未消费的距离,最后一个参数为输出参数,用于子view获取父view位置的偏移量。
返回值:(翻译出来可能有歧义,直接放原文)true if the event was dispatched,false if it Could not be dispatched.
最后,stopnestedScroll()方法与startnestedScroll(int axes)对应,用于结束嵌套滚动流程;而惯性滚动相关的两个方法与触摸滚动相关的两个方法类似,这里不再赘述。
nestedScrollingParent接口概述
//当开启、停止嵌套滚动时被调用 public Boolean onStartnestedScroll(View child,View target,int nestedScrollAxes); public void onnestedScrollAccepted(View child,int nestedScrollAxes); public void onStopnestedScroll(View target); //当触摸嵌套滚动时被调用 public void onnestedPreScroll(View target,int dx,int[] consumed); public void onnestedScroll(View target,int dxConsumed,int dyUnconsumed); //当惯性嵌套滚动时被调用 public Boolean onnestedPreFling(View target,float veLocityX,float veLocityY); public Boolean onnestedFling(View target,Boolean consumed);
从命名可以看出,这几个都是回调方法。当调用nestedScrollingChild中的方法时,nestedScrollingParent中与之相对应的方法就会被回调。方法之间的具体对应关系如下:
先看一下效果
先看一下布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://scheR_315_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_315_11845@as.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.qianmo.mynestedscrolling.view.MynestedScrollParent android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:BACkground="#f0f" android:text="上面的图片会被隐藏,而这个文字不会被隐藏"/> <com.qianmo.mynestedscrolling.view.MynestedScrollChild android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="123\n456\n789\n111\n222\n333\n444\n555\n666\n777\n888\n999\n14\n12\n13\n44\n55\n66\n77\n88\n99\n11\n22\n33\n44\n55\n66\n77\n88\n99\n77\n88\n88\n8\n88\n88\n" android:textColor="#f0f" android:textSize="20sp"/> </com.qianmo.mynestedscrolling.view.MynestedScrollChild> </com.qianmo.mynestedscrolling.view.MynestedScrollParent> </RelativeLayout>
布局文件只是简单的嵌套,MynestedScrollParent继承Linearlayout,并实现nestedScrollingParent接口,MynestedScrollChild同理,先来看看MynestedScrollChild这个类吧。
@H_571_0@mynestedScrollChild.javapackage com.qianmo.mynestedscrolling.view; import android.content.Context; import android.os.build; import android.support.Annotation.requiresApi; import android.support.v4.view.nestedScrollingChild; import android.support.v4.view.nestedScrollingChildHelper; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.LinearLayout; public class MynestedScrollChild extends LinearLayout implements nestedScrollingChild { private nestedScrollingChildHelper mnestedScrollingChildHelper; private final int[] offset = new int[2]; //偏移量 private final int[] consumed = new int[2]; //消费 privatE int lastY; privatE int showHeight; public MynestedScrollChild(Context context) { super(context); } public MynestedScrollChild(Context context,AttributeSet attrs) { super(context,attrs); } @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) { //第一次测量,因为布局文件中高度是wrap_content,因此测量模式为atmost,即高度不超过父控件的剩余空间 super.onMeasure(widthMeasureSpec,heightMeasureSpec); showHeight = getMeasuredHeight(); //第二次测量,对稿哦度没有任何限制,那么测量出来的就是完全展示内容所需要的高度 heightMeasureSpec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED); super.onMeasure(widthMeasureSpec,heightMeasureSpec); } @requiresApi(api = Build.VERSION_CODEs.LOLLIPOp) @Override public Boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { //按下 case MotionEvent.ACTION_DOWN: lastY = (int) event.getRawY(); break; //移动 case MotionEvent.ACTION_MOVE: int y = (int) (event.getRawY()); int dy = y - lastY; lastY = y; if (startnestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL) && dispatchnestedPreScroll(0,dy,consumed,offset)) //如果找到了支持嵌套滑动的父类,父类进行了一系列的滑动 { //获取滑动距离 int remain = dy - consumed[1]; if (remain != 0) { scrollBy(0,-remain); } } else { scrollBy(0,-dy); } break; } return true; } //限制滚动范围 @Override public void scrollTo(int x,int y) { int maxY = getMeasuredHeight() - showHeight; if (y > maxY) { y = maxY; } if (y < 0) { y = 0; } super.scrollTo(x,y); } //初始化Helper对象 private nestedScrollingChildHelper getScrollingChildHelper() { if (mnestedScrollingChildHelper == null) { mnestedScrollingChildHelper = new nestedScrollingChildHelper(this); mnestedScrollingChildHelper.setnestedScrollingEnabled(true); } return mnestedScrollingChildHelper; } //实现一下接口 @Override public void setnestedScrollingEnabled(Boolean enabled) { getScrollingChildHelper().setnestedScrollingEnabled(enabled); } @Override public Boolean isnestedScrollingEnabled() { return getScrollingChildHelper().isnestedScrollingEnabled(); } @Override public Boolean startnestedScroll(int axes) { return getScrollingChildHelper().startnestedScroll(axes); } @Override public void stopnestedScroll() { getScrollingChildHelper().stopnestedScroll(); } @Override public Boolean hasnestedScrollingParent() { return getScrollingChildHelper().hasnestedScrollingParent(); } @Override public Boolean dispatchnestedScroll(int dxConsumed,int[] offseTinWindow) { return getScrollingChildHelper().dispatchnestedScroll(dxConsumed,dyConsumed,dxUnconsumed,dyUnconsumed,offseTinWindow); } @Override public Boolean dispatchnestedPreScroll(int dx,int[] offseTinWindow) { return getScrollingChildHelper().dispatchnestedPreScroll(dx,offseTinWindow); } @Override public Boolean dispatchnestedFling(float veLocityX,Boolean consumed) { return getScrollingChildHelper().dispatchnestedFling(veLocityX,veLocityY,consumed); } @Override public Boolean dispatchnestedPreFling(float veLocityX,float veLocityY) { return getScrollingChildHelper().dispatchnestedPreFling(veLocityX,veLocityY); } }
主要是在OnTouchEvent中先后调用了startnestedScroll()和dispatchnestedPreScroll()方法,在借助Helper来完成nestedScrollingParent接口方法
@H_571_0@mynestedScrollParent.javapackage com.qianmo.mynestedscrolling.view; import android.content.Context; import android.support.v4.view.nestedScrollingParent; import android.support.v4.view.nestedScrollingParentHelper; import android.util.AttributeSet; import android.view.View; import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; /** * Created by wangjitao on 2017/2/14 0014. * 嵌套滑动机制父View */ public class MynestedScrollParent extends LinearLayout implements nestedScrollingParent { private ImageView img; private TextView tv; private MynestedScrollChild mynestedScrollChild; private nestedScrollingParentHelper mnestedScrollingParentHelper; privatE int imgHeight; privatE int tvHeight; public MynestedScrollParent(Context context) { super(context); } public MynestedScrollParent(Context context,attrs); init(); } private void init() { mnestedScrollingParentHelper = new nestedScrollingParentHelper(this); } //获取子view @Override protected void onFinishInflate() { img = (ImageView) getChildAt(0); tv = (TextView) getChildAt(1); mynestedScrollChild = (MynestedScrollChild) getChildAt(2); img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.onGlobalLayoutListener() { @Override public void onGlobalLayout() { if (imgHeight <= 0) { imgHeight = img.getMeasuredHeight(); } } }); tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.onGlobalLayoutListener() { @Override public void onGlobalLayout() { if (tvHeight <= 0) { tvHeight = tv.getMeasuredHeight(); } } }); } //在此可以判断参数target是哪一个子view以及滚动的方向,然后决定是否要配合其进行嵌套滚动 @Override public Boolean onStartnestedScroll(View child,int nestedScrollAxes) { if (target instanceof MynestedScrollChild) { return true; } return false; } @Override public void onnestedScrollAccepted(View child,int nestedScrollAxes) { mnestedScrollingParentHelper.onnestedScrollAccepted(child,target,nestedScrollAxes); } @Override public void onStopnestedScroll(View target) { mnestedScrollingParentHelper.onStopnestedScroll(target); } //先于child滚动 //前3个为输入参数,最后一个是输出参数 @Override public void onnestedPreScroll(View target,int[] consumed) { if (showImg(dy) || hideImg(dy)) {//如果需要显示或隐藏图片,即需要自己(parent)滚动 scrollBy(0,-dy);//滚动 consumed[1] = dy;//告诉child我消费了多少 } } //后于child滚动 @Override public void onnestedScroll(View target,int dyUnconsumed) { } //返回值:是否消费了fling @Override public Boolean onnestedPreFling(View target,float veLocityY) { return false; } //返回值:是否消费了fling @Override public Boolean onnestedFling(View target,Boolean consumed) { return false; } @Override public int getnestedScrollAxes() { return mnestedScrollingParentHelper.getnestedScrollAxes(); } //下拉的时候是否要向下滚动以显示图片 public Boolean showImg(int dy) { if (dy > 0) { if (getScrollY() > 0 && mynestedScrollChild.getScrollY() == 0) { return true; } } return false; } //上拉的时候,是否要向上滚动,隐藏图片 public Boolean hideImg(int dy) { if (dy < 0) { if (getScrollY() < imgHeight) { return true; } } return false; } //scrollBy内部会调用scrollTo //限制滚动范围 @Override public void scrollTo(int x,int y) { if (y < 0) { y = 0; } if (y > imgHeight) { y = imgHeight; } super.scrollTo(x,y); } }
①、在onStartnestedScroll()中判断参数target是哪一个子view以及滚动的方向,然后决定是否要配合其进行嵌套滚动
②、在onnestedPreScroll()中获取需要滚动的距离,根据情况决定自己是否要进行滚动,最后还要将自己滚动消费掉的距离存储在consumed数组中回传给child
就这样基本实现了,很简单有没有,再看看我们接下来要实现的效果,如图:
上源码 :MyNestedScrolling_code.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的Android中NestedScrolling滑动机制详解全部内容,希望文章能够帮你解决Android中NestedScrolling滑动机制详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。