欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中NestedScrolling滑动机制详解

程序员文章站 2023-12-18 12:02:04
1,如今nestedscrolling运用到很多地方了,要想好看一点的滑动变换,基本上就是使用这个来完成的,让我们来简单的了解一下。 2,nestedscrolling机...

1,如今nestedscrolling运用到很多地方了,要想好看一点的滑动变换,基本上就是使用这个来完成的,让我们来简单的了解一下。

2,nestedscrolling机制能够让父view和子view在滚动式进行配合,其基本流程如下:

  • 当子view开始滚动之前,可以通知父view,让其先于自己进行滚动;
  • 子view自己进行滚动;
  • 子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); 
public boolean startnestedscroll(int axes);

开启嵌套滚动流程(实际上是进行了一些嵌套滚动前准备工作)。

当找到了能够配合当前子view进行嵌套滚动的父view时,返回值为true(returns:true if a cooperative parent was found and nested scrolling has been enabled for the current gesture)。

public boolean dispatchnestedprescroll(int dx, int dy, int[] consumed, int[] offsetinwindow);

在子view自己进行滚动之前调用此方法,询问父view是否要在子view之前进行滚动。

此方法的前两个参数用于告诉父view此次要滚动的距离;而第三第四个参数用于子view获取父view消费掉的距离和父view位置的偏移量。

第一第二个参数为输入参数,即常规的函数参数,调用函数的时候我们需要为其传递确切的值。而第三第四个参数为输出参数,调用函数时我们只需要传递容器(在这里就是两个数组),在调用结束后,我们就可以从容器中获取函数输出的值。

如果parent消费了一部分或全部距离,则此方法返回true。

复制代码 代码如下:

public boolean dispatchnestedscroll(int dxconsumed, int dyconsumed, int dxunconsumed, int dyunconsumed, int[] offsetinwindow);

在子view自己进行滚动之后调用此方法,询问父view是否还要进行余下(unconsumed)的滚动。

前四个参数为输入参数,用于告诉父view已经消费和尚未消费的距离,最后一个参数为输出参数,用于子view获取父view位置的偏移量。

返回值:(翻译出来可能有歧义,直接放原文)true if the event was dispatched, false if it could not be dispatched.

public void stopnestedscroll();

最后,stopnestedscroll()方法与startnestedscroll(int axes)对应,用于结束嵌套滚动流程;而惯性滚动相关的两个方法与触摸滚动相关的两个方法类似,这里不再赘述。

nestedscrollingparent接口概述

//当开启、停止嵌套滚动时被调用

public boolean onstartnestedscroll(view child, view target, int nestedscrollaxes);

public void onnestedscrollaccepted(view child, view target, int nestedscrollaxes);

public void onstopnestedscroll(view target);

//当触摸嵌套滚动时被调用

public void onnestedprescroll(view target, int dx, int dy, int[] consumed);

public void onnestedscroll(view target, int dxconsumed, int dyconsumed, int dxunconsumed, int dyunconsumed);

//当惯性嵌套滚动时被调用

public boolean onnestedprefling(view target, float velocityx, float velocityy);

public boolean onnestedfling(view target, float velocityx, float velocityy, boolean consumed); 

从命名可以看出,这几个都是回调方法。当调用nestedscrollingchild中的方法时,nestedscrollingparent中与之相对应的方法就会被回调。方法之间的具体对应关系如下:

Android中NestedScrolling滑动机制详解

从上面的接口还有方法我们可以得出一些简单的流程

  • 调用child的startnestedscroll()来发起嵌套滑动流程(实质上是寻找能够配合child进行嵌套滚动的parent)。parent的onstartnestedscroll()会被调用,若此方法返回true,则onnestscrollaccepted()也会被调用。
  • chuld每次滚动前,可以先询问parent是否要滚动,即调用dispatchnestedscroll(),这时可以回调到parent的onnestedprescroll(),parent可以在这个回调中先于child滚动。
  • dispatchnestedprescroll()之后,child可以进行自己的滚动操作。

3,自定义nestedscrolling控件

先看一下效果

Android中NestedScrolling滑动机制详解  

先看一下布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.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这个类吧。

mynestedscrollchild.java

package 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 dyconsumed, int dxunconsumed, int dyunconsumed, int[] offsetinwindow) {

  return getscrollingchildhelper().dispatchnestedscroll(dxconsumed, dyconsumed, dxunconsumed, dyunconsumed, offsetinwindow);

 }
 @override

 public boolean dispatchnestedprescroll(int dx, int dy, int[] consumed, int[] offsetinwindow) {

  return getscrollingchildhelper().dispatchnestedprescroll(dx, dy, consumed, offsetinwindow);

 }
 @override

 public boolean dispatchnestedfling(float velocityx, float velocityy, 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接口方法

mynestedscrollparent.java

package 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, attributeset attrs) {

  super(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, view target, int nestedscrollaxes) {

  if (target instanceof mynestedscrollchild) {

   return true;

  }

  return false;

 }

 @override

 public void onnestedscrollaccepted(view child, view target, 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 dx, int dy, int[] consumed) {

  if (showimg(dy) || hideimg(dy)) {//如果需要显示或隐藏图片,即需要自己(parent)滚动

   scrollby(0, -dy);//滚动

   consumed[1] = dy;//告诉child我消费了多少

  }

 }

 //后于child滚动

 @override

 public void onnestedscroll(view target, int dxconsumed, int dyconsumed, int dxunconsumed, int dyunconsumed) {
 }


 //返回值:是否消费了fling

 @override

 public boolean onnestedprefling(view target, float velocityx, float velocityy) {

  return false;

 }

 //返回值:是否消费了fling

 @override

 public boolean onnestedfling(view target, float velocityx, float velocityy, 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);

 }

} 

mynestedscrollparent主要是实现一下功能

①、在onstartnestedscroll()中判断参数target是哪一个子view以及滚动的方向,然后决定是否要配合其进行嵌套滚动

②、在onnestedprescroll()中获取需要滚动的距离,根据情况决定自己是否要进行滚动,最后还要将自己滚动消费掉的距离存储在consumed数组中回传给child

就这样基本实现了,很简单有没有,再看看我们接下来要实现的效果,如图:

Android中NestedScrolling滑动机制详解

上源码 :mynestedscrolling_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: