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设置的联动方式,进行联动。

相关文章
|
4月前
|
存储 Java 数据库
Android数据存储:什么是Room Persistence Library?
Android数据存储:什么是Room Persistence Library?
49 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.
668 1
|
12月前
|
XML Java 开发工具
Android5.0新特性-Material Design
Android5.0新特性-Material Design
60 0
|
12月前
|
Android开发 开发者 UED
Android Design Support Library初探-更新中
Android Design Support Library初探-更新中
72 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
243 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(二)
60 0
Android Support Design Library之FloatingActionButton(二)
android.support.v4.app.Fragment$InstantiationException问题解决
android.support.v4.app.Fragment$InstantiationException问题解决
|
7天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
10天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
11天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。