VIew-CoordinatorLayout 笔记
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 笔记的更多相关文章
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...
- 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 ...
- (转)Qt Model/View 学习笔记 (六)——在views中选择数据项
在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...
- (转)Qt Model/View 学习笔记 (四)——创建新的Models
创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...
- (转)Qt Model/View 学习笔记 (三)——Model类
Model类 基本概念 在model/view构架中,model为view和delegates使用数据提供了标准接口.在Qt中,标准接口QAbstractItemModel类中被定义.不管数据在底层以 ...
- (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例
Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...
- (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介
Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...
- Android自定义View学习笔记(一)
绘制基础 参考:HenCoder Android 开发进阶: 自定义 View 1-1 绘制基础 Paint详解 参考:HenCoder Android 开发进阶: 自定义 View 1-2 Pain ...
随机推荐
- Java线程的概念
1. 计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...
- scrapy 学习笔记
1.scrapy 配合 selenium.phantomJS 抓取动态页面, 单纯的selemium 加 Firefox浏览器就可以抓取动态页面了, 但开启窗口太耗资源,而且一般服务器的linux 没 ...
- 第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash
1. UIImage imageWithContentsOfFile卡顿 [[UIImage alloc] initWithContentsOfFile 卡顿 2.uitableview scroll ...
- SpringMVC上传文件的三种方式(转)
直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/common ...
- POJ1753(位操作和枚举)
题目:http://poj.org/problem?id=1753 题意:一块4*4的棋盘,黑白块不规律分布,翻动一个色块,其上下左右,都会被翻动,知道全黑全白为止.输出最小次数,达不到则输出“Imp ...
- bzoj4691: Let There Be Light
如果原点能被一个光源照到,那么这两个点之间一定没有任何球.我们可以通过三分距离来确定某线段和球是否有交点. 注意到m非常小,于是我们可以枚举原点被哪些光源照到.由于\(O(2^{n}*m)\)会超时, ...
- CentOS7下安装配置MariaDB
参考: http://www.2cto.com/os/201504/394141.html http://outofmemory.cn/code-snippet/2533/mysql-create-d ...
- CDR VBA鼠标选择
Dim x As Double, y As Double, Shift As Long, b As Boolean, doc As Document Dim sel1 As Shape, sel2 A ...
- git 删除分支
1,删除本地分支 git branch -D 本地分支名 2,删除远程分支 git push origin --delete 远程分支名 注意:如果只删除了本地分支而没有删除远程分支,再新建一个相同名 ...
- CMS系统存储路径
CMS系统特点:前后端分离 index.html 首页文件index.php 管理后台的页面 api文件夹: 提供的接口 caches文件夹: 缓存文件 html文件夹: 生成的静态页面 phpcm ...