CoordinatorLayout的最簡入門

序言

本文力求用最短的篇幅,讓讀者明白CoordinatorLayout的用法。原理不作深入討論。

CoordinatorLayout是什么

由Google加入Android系統(tǒng)的一個新的布局容器。相當(dāng)于一個高級的FrameLayout。它通過Behavior的方式,使容器類的視圖能夠相互關(guān)聯(lián),協(xié)作,從而輕松地完成一些交互與動效。

CoordinatorLayout 如何接入

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

根據(jù)自己的complie版本,修改到對應(yīng)的版本即可。

CoordinatorLayout 如何使用

網(wǎng)上有很多文章結(jié)合xxxView,結(jié)合yyyView使用,仿佛<b>CoordinatorLayout</b>只能與部分結(jié)合使用,其實(shí)并非如此!那些奇奇怪怪的View,只是Android官方為我們寫好的示例。

<b>CoordinatorLayout</b>的核心是協(xié)調(diào),它能夠協(xié)調(diào)任何View之間的動作和效果。它以Behavior類作為連接view的橋梁。

實(shí)例演示

需求:界面中有一個Button背景是綠色。點(diǎn)擊它彈出一個Snackbar。當(dāng)Snackbar完全彈出時,Button背景變?yōu)榧t色。當(dāng)Snackbar準(zhǔn)備離開時,Button背景再度變?yōu)榫G色。

MyButtonBehavior

public class MyButtonBehavior extends CoordinatorLayout.Behavior<Button> {

    // 注意:如果沒有這個構(gòu)造方法,xml導(dǎo)入時將報錯。
    public MyButtonBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Button child, View dependency) {

        return dependency instanceof Snackbar.SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, Button child, View dependency) {
        if (dependency instanceof Snackbar.SnackbarLayout) {
            float y = ViewCompat.getTranslationY(dependency);
            if (y <= 5) {
                child.setBackgroundColor(0x44ff0000);
            } else {
                child.setBackgroundColor(0x4400ff00);
            }
        }
        return false;
    }
}

在這個例子中,我們最少需要MyButtonBehavior有三個方法。

  • parent 是 Button和Snackbar的容器
  • child 是Button
  • dependency 是Snackbar

因?yàn)樵诒纠校荁utton的背景色依賴Snackbar的位置變化。

布局文件

<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/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4400ff00"
        app:layout_behavior=".MyButtonBehavior"/>
</android.support.design.widget.CoordinatorLayout>

Activity

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Snackbar.make(view, "Hello world", Snackbar.LENGTH_LONG)
                        .setAction("cancel", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .show();
            }
        });
    }
}

以上就完成了需求中的效果。通過CoordinatorLayout和Behavior,將過去復(fù)雜的頁面回調(diào)封裝了起來,使代碼更加簡潔,開發(fā)效率也提高很多。

以上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容