Android Support Design Library之CoordinatorLayout(二)

简介: Android Support Design Library之CoordinatorLayout(二)

3.自定义一个Behavior


㈠继承Behavior

public class LYJBehavior extends CoordinatorLayout.Behavior {
    public LYJBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}


㈡2种引用方法:


①在XML布局中直接引用


app:layout_behavior=“你的Behavior包含包名的类名”


②另外一种方法如果你的自定义View默认使用一个Behavior。在你的自定义View类上添加@DefaultBehavior(你的Behavior.class)这句注解。你的View就默认使用这个Behavior,代码如下:

@DefaultBehavior(AppBarLayout.Behavior.class)
public class LYJLayout extends LinearLayout {}


㈢生成Behavior后第一件事就是确定依赖关系。重写Behavior的这个方法来确定你依赖哪些View。


@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
 return super.layoutDependsOn(parent, child, dependency);
}


child 是指应用behavior的View ,dependency 担任触发behavior的角色,并与child进行互动。确定你是否依赖于这个ViewCoordinatorLayout会将自己所有View遍历判断。如果确定依赖。这个方法很重要。


㈣当所依赖的View变动时会回调这个方法。

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    return super.onDependentViewChanged(parent, child, dependency);
}


4.使用自定义的Behavior


自定义一个属性:


<declare-styleable name="Follow">
        <attr name="target" format="reference"/>
</declare-styleab<strong>le></strong>


使用代码:

public class LYJBehavior extends CoordinatorLayout.Behavior {
    private int targetId;
    public LYJBehavior(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);//获取联动ID
            }
        }
        a.recycle();
    }
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency.getId() == targetId;
    }
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        child.setY(dependency.getY()+dependency.getHeight());//child不管dependency怎么移动,其都在dependency下面
        return true;
    }
}


XML中的代码如下:

<?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <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="com.example.liyuanjing.tablayoutdemo.LYJBehavior"
        app:target="@id/first"
        android:background="@android:color/holo_green_light"/>
</android.support.design.widget.CoordinatorLayout>


app:target:中传入ID,自定义behavior就会获取联动的View。然后根据你在onDependentViewChanged设置的联动方式,进行联动。

相关文章
|
18天前
|
存储 Java 数据库
Android数据存储:什么是Room Persistence Library?
Android数据存储:什么是Room Persistence Library?
57 0
|
Android开发
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
730 1
|
XML Java 开发工具
Android5.0新特性-Material Design
Android5.0新特性-Material Design
64 0
|
Android开发 开发者 UED
Android Design Support Library初探-更新中
Android Design Support Library初探-更新中
75 0
|
Android开发
Unrecognized Android Studio (or Android Support plugin for IntelliJ IDEA) version ‘202.7660.26.42.74
Unrecognized Android Studio (or Android Support plugin for IntelliJ IDEA) version ‘202.7660.26.42.74
260 0
Unrecognized Android Studio (or Android Support plugin for IntelliJ IDEA) version ‘202.7660.26.42.74
|
开发工具 Android开发
Android Support Design Library之FloatingActionButton(二)
Android Support Design Library之FloatingActionButton(二)
62 0
Android Support Design Library之FloatingActionButton(二)
|
Android开发 API
Android Design Support Library使用详解——Snackbar
Google在2015 I/O大会上,给我们带来了更加详细的Material Design规范,同时也引入了Android Design Support Library,为我们提供了基于Material设计规范的控件。
1095 0
|
API Android开发 容器
Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个support库里面,Google给我们提供了更加规范的MD设计风格的控件。
841 0
|
1天前
|
Android开发 开发者 UED
探索安卓应用开发中的UI设计趋势
随着移动应用市场的不断发展和用户需求的变化,安卓应用的UI设计趋势也在不断演进。本文将深入探讨当前安卓应用开发中的UI设计趋势,包括暗黑模式、原生化设计、动效设计等方面的发展趋势,为开发者提供参考和启发。
|
2天前
|
安全 网络安全 量子技术
网络安全与信息安全:漏洞、加密技术与安全意识的探索安卓应用开发中的内存管理策略
【5月更文挑战第31天】随着互联网的普及,网络安全问题日益严重。本文将深入探讨网络安全漏洞、加密技术以及安全意识等方面的问题,以期提高公众对网络安全的认识和防范能力。