<Android 基础(二十)> CoordinatorLayout Behavior
介绍
Interaction behavior plugin for child views of {@link CoordinatorLayout}.
A Behavior implements one or more interactions that a user can take on a child view.
These interactions may include drags, swipes, flings, or any other gestures.@param < V > The View type that this Behavior operates on
交互行为主要用于CoordinatorLayout的子View。一个Behavior可以在一个字View上实现一个或者多个交互内容。这些交互可能包括拖拽,滑动,滚动或者是其他手势。
参数V代表这个Behavior可以操作的视图类型
类结构
初步实战
关注两个方面:
1、实现child view 监听另外一个child view的状态变化,例如大小、位置、显示状态等
关注layoutDependsOn和onDependentViewChanged方法。
2、实现child view监听CoordinatorLayout内NestedScrollingChild的接口实现类的滑动状态
关注onStartNestedScroll和onNestedPreScroll方法。如上一篇博文中提到的NestedScrollView,而不能使用普通的ScrollView
第一种情况,监听View的状态
位置跟随
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="mraz.com.coordinatorlayoutdemo.ScrollingActivity">
<TextView
android:id="@+id/depentent"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="top|right"
android:background="#ffee00"
android:gravity="center"
android:text="auto"
android:textColor="@android:color/holo_red_dark"
app:layout_behavior="mraz.com.coordinatorlayoutdemo.FollowingBehavior" />
</android.support.design.widget.CoordinatorLayout>
Behavior
package mraz.com.coordinatorlayoutdemo;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Mraz on 2016/8/15.
*/
public class FollowingBehavior extends CoordinatorLayout.Behavior<View> {
public FollowingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return (dependency.getId() == R.id.depentent);//依赖左边的View
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
int offsetX = dependency.getTop() - child.getTop();//拿到左边View和右边view的顶部差距
ViewCompat.offsetTopAndBottom(child, offsetX);//移动
return true;
}
}
实际效果
第二种情况,监听滚动状态
CoordinatorLayout实现NestedScrollingParent接口,
NestedScrollView实现NestedScrollingParent, NestedScrollingChild, ScrollingView接口
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView
说白了就是一个加强的ScrollView,但是实现的这几个接口是怎么回事呢?一个个的看下
NestedScrollingParent
| Interface | Meaning |
|---|---|
| onStartNestedScroll | child执行startNestedScroll方法的时候会调用,返回true代表代表要和child联动 |
| onNestedScrollAccepted | onStartNestedScroll执行完成并返回true之后执行,用于做联动前的准备 |
| onStopNestedScroll | nested scroll结束后执行 |
| onNestedScroll | nested scroll进行中时执行,传递的参数是消耗了和没有消耗的滚动距离 |
| onNestedPreScroll | neseted scroll进行并且target没有消耗滚动时执行,nested scrolling执行dispatchNestedPreScroll后执行该方法 |
| onNestedFling | 请求一个滚动结束后的fling |
| onNestedPreFling | target view没有消耗flinger之前执行 |
| getNestedScrollAxes | 返回NestedScrollingParent的滚动轴 |
NestedScrollingChild
| Interface | Meaning |
|---|---|
| setNestedScrollingEnabled | 设置视图是否可以nested scrolling |
| isNestedScrollingEnabled | 获取当前view是否可以nested scrolling |
| startNestedScroll | 开始执行nested scroll,沿着给点的轴方向,返回true,代表找到了parent |
| stopNestedScroll | 停止nested scroll |
| hasNestedScrollingParent | 判断nested scrolling是否存在parent |
| dispatchNestedScroll | 派发一个滚动给parent |
| dispatchNestedPreScroll | 在没有消耗之前派发给parent,也就是parent有可能会在child之前消耗 |
| dispatchNestedFling | 派发fling给parent |
| dispatchNestedPreFling | 消耗之前派发给parent |
ScrollingView
| Interface | Meaning |
|---|---|
| computeHorizontalScrollRange | 计算横向滚动的范围 |
| computeHorizontalScrollOffset | 计算横向偏移量 |
| computeHorizontalScrollExtent | 计算横向额外距离 |
| computeVerticalScrollRange | 滚动视图的可滚动范围是所有子元素的高度 |
| computeVerticalScrollOffset | 计算垂直方向滚动条的滑块的偏移。此值用来计算滚动条轨迹的滑块的位置 |
| computeVerticalScrollExtent | 计算纵向额外距离 |
关于NestedScrollingParent和NestedScrollingChild,存在两个Helper类
基本上方法和上面的比较类似,不赘述
看实例:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scrollbars="vertical"
tools:context="mraz.com.coordinatorlayoutdemo.ScrollingActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#73ff00"
android:gravity="center"
android:text="depentent"
android:textColor="@android:color/black" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_behavior="mraz.com.coordinatorlayoutdemo.MyScrollingBehavior">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ff00aa"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
app:layout_behavior="mraz.com.coordinatorlayoutdemo.MyScrollingBehavior">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:background="#ffe600"
android:gravity="center"
android:text="right"
android:textColor="@android:color/black" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
3条杠杠~
Behavior
package mraz.com.coordinatorlayoutdemo;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.View;
public class MyScrollingBehavior extends CoordinatorLayout.Behavior {
public MyScrollingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;//只对垂直滚动感兴趣
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
int leftScrolled = target.getScrollY();//获取左侧滚动距离
child.setScrollY(leftScrolled);//设置联动
}
@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY) {
((NestedScrollView) child).fling((int) velocityY);//松手后速度传递下去
return true;
}
}
实际效果
备注
参考博客:
http://blog.csdn.net/qibin0506/article/details/50290421
<Android 基础(二十)> CoordinatorLayout Behavior的更多相关文章
- Bootstrap <基础二十九>面板(Panels)
Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...
- Bootstrap <基础二十八>列表组
列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...
- Bootstrap <基础二十六>进度条
Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...
- Bootstrap <基础二十五>警告(Alerts)
警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...
- Bootstrap<基础二十四> 缩略图
Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...
- Bootstrap <基础二十二>超大屏幕(Jumbotron)
Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...
- Bootstrap<基础二十> 标签
Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...
- shell基础二十篇 一些笔记
shell基础二十篇 转自 http://bbs.chinaunix.net/thread-452942-1-1.html 研讨:Bash 内建命令 read (read命令更具体的说明见博客收藏的一 ...
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- Android进阶(二十)AndroidAPP开发问题汇总(四)
· Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...
随机推荐
- element-ui多层嵌套表格数据删除
很多表格都要一个移除的功能,所谓移除,就是前端把表格的数据删除,普通的表格删除很简单,调用数据的删除方法就行.但是当表格是多层的嵌套类型时,就不能再使用普通的删除方法了.下面介绍一种自己在项目中用的方 ...
- zookeeper 数据节点的增删改查
1.连接服务端 [root@localhost bin]# ./zkCli.sh -server 127.0.0.1:2181 Connecting to 127.0.0.1:2181 2018-05 ...
- CSAPP阅读笔记-数组分配与访问-来自第三章3.8的笔记-P176-P183
这一节比较简单,仅记录几个比较重要的点: 1.C语言允许对指针进行运算,计算出的值会根据该指针引用的数据类型大小进行伸缩. 例子: 其中,xE是数组的起始地址.注意,指针运算时,若最终结果为指针,则指 ...
- List与IList的区别
在我看一个源程序的时候看到这个例子使用了IList<T>返回类型,因为上午刚刚总结过List<T>的详细用法,突然出现了IList<T>,感觉很奇怪,于是上网搜集了 ...
- (转)Http状态码301和302概念简单区别及企业应用案例
Http状态码301和302的区别及企业应用案例 原文:http://blog.51cto.com/oldboy/1774260 1.什么是301重定向? 301重定向/跳转一般,表示本网页永久性转移 ...
- 如何获取用户的地理位置? && html5 地理位置
推荐网站 https://html5demos.com/geo/ 我们有时候可能希望首先获得用户的地理位置,然后根据不同的地理位置(更具针对性地)推送不同的信息等等. 下面这段代码就可以在你有jQue ...
- MySQL多表更新
多表更新:参照另外的表来更新本表的内容 table_reference {[inner | cross] join | {left | right} [outer] join} 内连接.左外连接.右 ...
- 用泛型T替代object做为万能参数传递
using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...
- PyCharm鼠标右键不显示Run unittest方法
PyCharm鼠标右键不显示Run unittest方法 PyCharm是一个用来写python代码的IDE,很好用.在其中建立了unittest类后,鼠标点击某个test方法后,菜单中会显示Run ...
- Java Service Wrapper 发布Java程序或者jar包为Windows服务
下载Windows版本:http://nchc.dl.sourceforge.net/sourceforge/wrapper/wrapper-windows-x86-32-3.2.3.zip 现在目前 ...