CoordinatorLayout

协调者:一般会是两个控件,一个Dependency一个child ,CoordinatorLayout的主要功能就是协调这两个控件,使child跟随Dependency的布局变化而变化(比如:位置,大小等)。其中变化的规则,则是由一个CoordinatorLayout.Behavior来决定。demo:

一:布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>     <com.kxl.mydemo.view.CoordinaterDependencyView
        android:id="@+id/coor_layout"
        android:layout_width="60dp"
        android:text="Dependency"
        android:textColor="#000000"
        android:layout_height="60dp"
        android:background="#55ff55"
/>     <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff5555"
        android:text="child"
        app:layout_behavior="com.kxl.mydemo.coordinator.MoveBehavior"
/> </android.support.design.widget.CoordinatorLayout>

二。自定义的一个,可移动的view:com.kxl.mydemo.view.CoordinaterDependencyView

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
 * Created by kxl on 2016/10/18.
 */
public class CoordinaterDependencyView extends TextView {
    private  String TAG = "CoordinaterDependencyView";
    private int width;
    private int height;
    int lastx;
    int lasty;
    public CoordinaterDependencyView(Context context) {
        super(context);
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int displaywidth =  displayMetrics.widthPixels;
        int displayheight =  displayMetrics.heightPixels;
        Log.i(TAG,"displaywidth:"+displaywidth+"  displayheight:"+displayheight);
    }
    public CoordinaterDependencyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getRawX();
        int y = (int)event.getRawY();
        Log.i(TAG,"onTouchEvent x:"+x+"  y:"+y);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
                int nx = layoutParams.leftMargin+x-lastx;
                int ny = layoutParams.topMargin+y-lasty;
                layoutParams.leftMargin = nx;
                layoutParams.topMargin = ny;
                setLayoutParams(layoutParams);
                requestLayout();
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        lastx = x;
        lasty = y;
        return true;
    }
}

三:协调规则。Behavior:

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup; import com.kxl.mydemo.view.CoordinaterDependencyView;
http://www.90168.org/
/**
 * Created by kxl on 2016/10/18.
 */
public class MoveBehavior extends CoordinatorLayout.Behavior<View> {
    int  width;
    public MoveBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics display = context.getResources().getDisplayMetrics();
        width  = display.widthPixels;
    }
    /**
     * @return 返回是否是child依赖的布局
     */
   
@Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof CoordinaterDependencyView;
    }
    /**
     *  dependency控件有变化时,会调用这个方法
     */
   
@Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        ViewGroup.MarginLayoutParams pa = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
        pa.leftMargin = width  - dependency.getLeft();
        pa.topMargin =  dependency.getTop();
        child.setLayoutParams(pa);
        return true;
    }
}

VIew-CoordinatorLayout 笔记的更多相关文章

  1. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  2. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  3. Understanding Scroll Views 深入理解 scroll view 读书笔记

    Understanding Scroll Views 深入理解 scroll view  读书笔记   It may be hard to believe, but a UIScrollView is ...

  4. iphone/ipad关于size, frame and bounds总结和UIScroll view学习笔记

    1. iphone/ipad大小 Device Screen dimensions(in points) iphone and ipod 320 X 480 ipad 768 X 1024 2. UI ...

  5. (转)Qt Model/View 学习笔记 (六)——在views中选择数据项

    在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...

  6. (转)Qt Model/View 学习笔记 (四)——创建新的Models

    创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...

  7. (转)Qt Model/View 学习笔记 (三)——Model类

    Model类 基本概念 在model/view构架中,model为view和delegates使用数据提供了标准接口.在Qt中,标准接口QAbstractItemModel类中被定义.不管数据在底层以 ...

  8. (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例

    Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...

  9. (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介

    Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...

  10. Android自定义View学习笔记(一)

    绘制基础 参考:HenCoder Android 开发进阶: 自定义 View 1-1 绘制基础 Paint详解 参考:HenCoder Android 开发进阶: 自定义 View 1-2 Pain ...

随机推荐

  1. C#如何获取本机网络IP地址

    在开发过程中我们经常会碰到需要IP地址,用来记录用户上次登录的时间地址,或者sokect网络编程等等,下面介绍两种方式: 1. public static string GetIP() { retur ...

  2. MarkdownPad 2 在win10下出错:HTML 渲染错误(This view has crashed) 的解决办法 + MarkdownPad2.5 注册码

    首先附上MarkdownPad2.5的注册码. 邮箱:Soar360@live.com 授权密钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImD ...

  3. required - HTML5里的input标签的required属性提示文字修改

    input 里面增加这样的语句: <input type="text" placeholder="您的姓名" required oninvalid=&qu ...

  4. http://www.microsoft.com/en-pk/download/details.aspx?id=40762

    http://www.microsoft.com/en-pk/download/details.aspx?id=40762

  5. word20161216

    object / 对象 object identifier / 对象标识符 offline / 脱机  OLE on-disk catalog / 磁盘目录 on-media catalog / 媒体 ...

  6. LaTeX简单使用方法

    Content LaTeX的用途 LaTeX文件布局 LaTeX的文档格式 公式环境 图的排版 表格的排版 有序列表和无序列表 引用 伪代码 参考文献 LaTeX的用途 LaTeX是一种基于TeX的排 ...

  7. Unity MonoDevelop一打开未响应

    在学习Untiy的时候,使用内置的MonoDevelop开发工具.本来就不好用,经常出现未响应的情况,然后重启解决.终于有一次莫名其妙的崩溃了,在Unity打开该IDE就未响应,但直接打开MonoDe ...

  8. Unity 编译错误记录

    1. 相关代码: NetworkView.RPC ("ReceiveMessage", RPCMode.All, message); 编译输出: Assets/cs/ClientC ...

  9. 163邮件出错:不允许使用邮箱名称。 服务器响应为: authentication is required,smtp7,C8CowEDpS0+Uke9VvSmXBg--.546S2 1441763733

    原因:用163邮箱发邮件,需开启smtp服务,开启服务时,要求使用客户端授权码. 在.net中,使用smtp发邮件,在验证中使用的密码,是上面所讲的客户端授权码,而不是注册和web登录时用的邮箱密码. ...

  10. appium for hybrid app 处理webview

    之前研究了一段时间的appium for native app 相应的总结如下:                                            appium测试环境搭建 :ht ...