CoordinatorLayout 使用综述系列(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zl18603543572/article/details/78210147

CoordinatorLayout 使用综述系列(一)

在 gradle 文件中引入 meterial design 库:

compile 'com.android.support:design:22.2.0'

CoordinatorLayout 实现了多种Material Design中提到的滚动效果

1 CoordinatorLayout 与 FloatingActionButton 结合使用

<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.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_launcher" />

</android.support.design.widget.CoordinatorLayout>

activity 中

findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show();
    }
});

2 CoordinatorLayout 与 其他控件(例如 Button) 结合使用

<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">

    <Button
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:text="click"/>

</android.support.design.widget.CoordinatorLayout>

弹出的Snackbar会将Button覆盖,也就是CoordinatorLayout 不能正常工作。这是因为 View 没有默认的CoordinatorLayout.Behavior 的实现,
那么我们的解决方案就是自定义一个CoordinatorLayout.Behavior来控制Button的响应

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * class infation
 */
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<Button> {
    public FloatingActionButtonBehavior() {
    }

    public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {
        //如果我们想监听改变,就让方法返回 true。在例子中,我们只想监听 Snackbar 对象的改变
        return dependency instanceof Snackbar.SnackbarLayout;
    }
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, Button child, View dependency) {
        /**
         * 当 CoordinatorLayout 中的 view 每次发生变化时,onDependentViewChanged 方法都会被调用。
         * 在这个方法中,我们要读取当前 Snackbar 的状态。
         * 当Snackbar 显示的时候,我们想把 Button 也移上来。
         * 要实现这样的目的,我们需要把 Button 的 Y 坐标设置为Snackbar 的高度。要得到正确的转换值,我们需要从转化的 Y 值中减去 Snackbar 的高度
         */
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
}

然后将写好的 FloatingActionButtonBehavior 设置给我们所要变动的Button 

<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">

    <Button
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:text="click"/>

</android.support.design.widget.CoordinatorLayout>


所有通过上述的自定义Behavior  我们就可以做到定义任想要改变的控件移动

猜你喜欢

转载自blog.csdn.net/zl18603543572/article/details/78210147