CoordinatorLayout 上的一些布局技巧
来源:互联网 发布:cocos2dx mac环境搭建 编辑:程序博客网 时间:2024/06/15 13:37
Behavior是Android新出的Design库里新增的布局概念。Behavior只有是CoordinatorLayout的直接子View才有意义。可以为任何View添加一个Behavior。Behavior是一系列回调。让你有机会以非侵入的为View添加动态的依赖布局,和处理父布局(CoordinatorLayout)滑动手势的机会。不过官方只有少数几个Behavior的例子。对于理解Behavior实在不易。开发过程中也是很多坑,下面总结一下CoordinatorLayout与Behavior。
依赖
首先自定义一个Behavior。
public class MyBehavior extends CoordinatorLayout.Behavior{ public MyBehavior(Context context, AttributeSet attrs) { super(context, attrs); } }
一定要重写这个构造函数。因为CoordinatorLayout源码中
parseBehavior()
函数中直接反射调用这个构造函数。static final Class<?>[] CONSTRUCTOR_PARAMS = new Class<?>[] { Context.class, AttributeSet.class};
下面反射生成Behavior实例在实例化CoordinatorLayout.LayoutParams时:
final Class<Behavior> clazz = (Class<Behavior>) Class.forName(fullName, true, context.getClassLoader());c = clazz.getConstructor(CONSTRUCTOR_PARAMS);c.setAccessible(true);constructors.put(fullName, c);return c.newInstance(context, attrs)
在任意View中添加:
app:layout_behavior=“你的Behavior包含包名的类名”
然后CoordinatorLayout就会反射生成你的Behavior。
另外一种方法如果你的自定义View默认使用一个Behavior。
在你的自定义View类上添加@DefaultBehavior(你的Behavior.class)这句注解。
你的View就默认使用这个Behavior。就像AppBarLayout一样。@DefaultBehavior(AppBarLayout.Behavior.class)public class AppBarLayout extends LinearLayout {}
生成Behavior后第一件事就是确定依赖关系。重写Behavior的这个方法来确定你依赖哪些View。
@Overridepublic boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { return dependency.getId() == R.id.first;}
child 是指应用behavior的View ,dependency 担任触发behavior的角色,并与child进行互动。
确定你是否依赖于这个View。CoordinatorLayout会将自己所有View遍历判断。
如果确定依赖。这个方法很重要。当所依赖的View变动时会回调这个方法。@Overridepublic boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { return true;}
下面这个例子:
<declare-styleable name="Follow"> <attr name="target" format="reference"/> </declare-styleable>
先自定义target这个属性。
public class FollowBehavior extends CoordinatorLayout.Behavior { private int targetId; public FollowBehavior(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Follow); for (int i = 0; i < a.getIndexCount(); i++) { int attr = a.getIndex(i); if(a.getIndex(i) == R.styleable.Follow_target){ targetId = a.getResourceId(attr, -1); } } a.recycle(); } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { child.setY(dependency.getY()+dependency.getHeight()); return true; } @Override public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { return dependency.getId() == targetId; }}
xml中:
<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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <View android:id="@+id/first" android:layout_width="match_parent" android:layout_height="128dp" android:background="@android:color/holo_blue_light"/> <View android:id="@+id/second" android:layout_width="match_parent" android:layout_height="128dp" app:layout_behavior=".FollowBehavior" app:target="@id/first" android:background="@android:color/holo_green_light"/></android.support.design.widget.CoordinatorLayout>
效果是不管first怎么移动。second都会在他下面。
01.gif滑动
Behavior最大的用处在于对滑动事件的处理。就像CollapsingToolbarLayout的那个酷炫效果一样。
主要是这3个方法,所依赖对象的滑动事件都将通知进来:
@Overridepublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) { return true;//这里返回true,才会接受到后续滑动事件。}@Overridepublic void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {//进行滑动事件处理}@Overridepublic boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) {//当进行快速滑动 return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);}
注意被依赖的View只有实现了NestedScrollingChild接口的才可以将事件传递给CoordinatorLayout。
但注意这个滑动事件是对于CoordinatorLayout的。所以只要CoordinatorLayout有NestedScrollingChild就会滑动,他滑动就会触发这几个回调。无论你是否依赖了那个View。
下面就是一个简单的View跟随ScrollView滑入滑出屏幕的例子。可以是Toolbar或其他任何View。public class ScrollToTopBehavior extends CoordinatorLayout.Behavior<View>{ int offsetTotal = 0; boolean scrolling = false; public ScrollToTopBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) { return true; } @Override public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { offset(child, dyConsumed); } public void offset(View child,int dy){ int old = offsetTotal; int top = offsetTotal - dy; top = Math.max(top, -child.getHeight()); top = Math.min(top, 0); offsetTotal = top; if (old == offsetTotal){ scrolling = false; return; } int delta = offsetTotal-old; child.offsetTopAndBottom(delta); scrolling = true; }}
xml中:
<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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false" tools:context=".MainActivity"> <android.support.v4.widget.NestedScrollView android:id="@+id/second" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="128dp" style="@style/TextAppearance.AppCompat.Display3" 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:background="@android:color/holo_red_light"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> <View android:id="@+id/first" android:layout_width="match_parent" android:layout_height="128dp" app:layout_behavior=".ScrollToTopBehavior" android:background="@android:color/holo_blue_light"/></android.support.design.widget.CoordinatorLayout>
当NestedScrollView滑动的时候,first也能跟着滑动。toolbar和fab的上滑隐藏都可以这样实现。
02.gif事件处理
这2个回调与View中的事件分发是一样的。所有Behavior能在子View之前收到CoordinatorLayout的所有触摸事件。可以进行拦截,如果拦截事件将不会流经子View。因为这2个方法都是在CoordinatorLayout的 回调中
@Overridepublic boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) { return super.onInterceptTouchEvent(parent, child, ev);}@Overridepublic boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) { return super.onTouchEvent(parent, child, ev);}
AppBarLayout的收缩原理分析
示例中给可滑动View设的Behavior是
@string/appbar_scrolling_view_behavior
(android.support.design.widget.AppBarLayout$ScrollingViewBehavior)。
ScrollingViewBehavior的源码不多,看得出唯一的作用是把自己放到AppBarLayout的下面...(不能理解为什么叫ScrollingViewBehavior
)
所有View都能使用这个Behavior。AppBarLayout自带一个Behivior。直接在源码里注解声明的。这个Behivior也只能用于AppBarLayout。
作用是让他根据CoordinatorLayout上的滚动手势进行一些效果(比如收缩)。与ScrollingViewBehavior是无关的,加不加ScrollingViewBehavior不影响收缩。
只不过只有某些可滑动View才会把滑动事件响应给CoordinatorLayout才能继而响应给AppBarLayout。
作者:Jude95
链接:http://www.jianshu.com/p/a506ee4afecb
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- CoordinatorLayout 上的一些布局技巧
- 协调布局CoordinatorLayout的一些方法
- 一些小技巧在布局上
- 写出高效清晰Layout布局文件的一些技巧--------自定义CoordinatorLayout:实现淘宝和QQ ToolBar透明渐变效果-------自定义Coordi快速返回效果
- CoordinatorLayout布局的简单实用
- CoordinatorLayout布局的使用方式
- CoordinatorLayout布局的使用方式
- CoordinatorLayout父布局的Behavior
- Android布局优化的一些小技巧
- 关于布局的一些小技巧
- CoordinatorLayout +RecyclerView+加载不同布局的item
- FrameLayout布局的增强版—CoordinatorLayout
- Android中CoordinatorLayout(协调布局)的使用
- CoordinatorLayout:CollapsingToolbarLayout折叠效果的布局容器
- CoordinatorLayout +RecyclerView+加载不同布局的item
- CoordinatorLayout 布局系列 Behavior 的自定义
- 高大上网站布局的三个技巧
- 关于CoordinatorLayout的一些使用经验
- 文件上传方式2
- 不同用户设置ssh信任关系不输入密码执行scp 命令
- [译]Multi-Gradient shape
- Tarjin算法求桥和割点模板
- Python基于pandas的数据处理
- CoordinatorLayout 上的一些布局技巧
- Java-MongoDB入门级小程序,方便直接上手使用
- 域名解析相关备忘
- greendao的使用
- 求100-200之间素数的三种方法
- ORM系列之三:Dapper
- 2017年第9届中国(江苏)国际农业机械展览会会刊(参展商名录)
- Window10安装TensorFlow(GPU)与可行性测试
- 关于设备描述表(DC)的详解