学习资料:
- dodo_lihao同学的CoordinatorLayout使用(三):NestedScrollView & 嵌套滑动事件
本篇记录学习Behavior相关的嵌套滑动事件,学习嵌套事件其实本质就是学习了解NestedScrolling机制
1.NestedScrolling机制 <p>
随便一百度,感觉前几篇出来的博客质量都蛮不错的
- Jlog大神的Android NestedScrolling 实战
- 鸿洋大神的Android NestedScrolling 机制完全解析带你玩转嵌套滑动
- tuacy同学的Android 嵌套滑动分析
NestedScrolling机制是在5.0时推出,其作用就是提供一套父容器和childview滑动交互机制,自然的,核心有两个东西:
- NestedScrollingChild,嵌套滑动子接口
- NestedScrollingParent,嵌套滑动父接口
个人理解:
实现了NestedScrollingChild接口的可滑动childview,在准备进入滑动状态后,可以根据需求,在滑动时主动向实现了NestedScrollingParent接口的父容器传递消息,而父容器就根据收到的消息,再指定对象来作出响应
我所谓的传递和接收消息,其实就是接口回调的过程,具体的中间工作原理过程可以看前面给出的博客,现在的要求是先学会使用
CoordinatorLayout实现了NestedScrollingParent接口,RecyclerView实现了NestedScrollingChild,而NestedScrollView则实现了两个接口
这样就可以理解在CoordinatorLayout中,想要配合AppBarLayout实现折叠隐藏ToolBar时,ListView和ScrollView不能起到效果的原因。因为ListView和ScrollView没有实现NestedScrollingChild,并不能将滑动状态主动告诉CoordinatorLayout,自然而然的,也就无法作出回应
NestedScrollView实现了两个接口,也就是说既可以当作传递消息的childview,也可以当作接收消息的父容器
CoordinatorLayout作为一个实现了NestedScrollingParent接口的父容器,接收到消息时,根据消息真正作出响应消息的是绑定了Behavior的对象。至于消息如何被处理消费,则要看具体的Behavior子类中,依赖对象和绑定对象的逻辑操作
2. 简单例子 <p>
说明:
例子的思路和代码是直接从dodo_lihao同学的博客中照搬来的,感觉他博客中的案例很容易表现出NestedScrolling机制在Behavior中的使用特点

效果就是左面一列滑动并不会影响右面的一列,而有面一列滑动时,会影响左面,左面跟随右面而滑动
2.1 布局代码
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout_behavior=".behavior.NestedBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="A\\nB\\nC\\nD\\nE\\nF\\nG\\nH\\nI\\nJ\\nK\\nL\\nM\\nN\\nO\\nP\\nQ\\nR\\nS\\nT\\nU\\nV\\nW\\nX\\nY\\nZ"
android:textColor="@color/colorAccent"
android:textSize="30sp" />
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="A\\nB\\nC\\nD\\nE\\nF\\nG\\nH\\nI\\nJ\\nK\\nL\\nM\\nN\\nO\\nP\\nQ\\nR\\nS\\nT\\nU\\nV\\nW\\nX\\nY\\nZ"
android:textColor="@color/colorAccent"
android:textSize="30sp" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
布局中就是两个NestedScrollView,内部各自有一个TextView,左面的NestedScrollView使用了app:layout_behavior=".behavior.NestedBehavior"作为绑定对象
2.2 NestedBehavior <p>
思路:
- NestedScrollView直接会发送事件
- CoordinatorLayout也就是外面parent的会自动接收
- 一个Behavior子类来消费
public class NestedBehavior extends CoordinatorLayout.Behavior<NestedScrollView> {
public NestedBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 返回值就是依赖childView的滑动准备状态
* 告诉CoordinatorLayout,要准备开始滑动
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
/**
* 滑动过程中回调
* CoordinatorLayout接收到消息后,事件真正被消费的回调方法
*/
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, NestedScrollView child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
int scrollY = target.getScrollY();
child.setScrollY(scrollY);
}
/**
* 返回值就是依赖的childView的是否处于Fling状态
*
*/
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, NestedScrollView child, View target, float velocityX, float velocityY, boolean consumed) {
child.fling((int) velocityY);
return true;
}
}
NestedScrollView既可以接受消息又可以传递消息,无需绑定
3. 最后 <p>
NestedScrolling机制以后用到,还需要再根据实际需求深入学习,现在也只是入门学习了解
首付一半,免息打白条分一年,在京东买的MacBook Pro乞丐版也用了两天了,逐渐适应了系统,感觉蛮好用的,就是开了Android Studio后,动不动就热乎乎的有点不爽。以后得努力学习,挣多点钱赶紧还白条。。。
本人很菜,有错误请指出
共勉 :)
