簡體   English   中英

協調器布局自定義布局行為永遠不會被調用

[英]Coordinator layout custom layout behavior never getting called

首先,我想以協調員布局缺乏知識為前提。 我只是按照我在網上找到的教程,很好奇為什么我的行為不起作用。

協調器布局內的子視圖是否必須是應用欄布局? 或者你能在那里放任何視圖。

此外,當我定義res-auto命名空間時,它沒有給我layout_behavior的選項。 通常android studio會自動完成,如果一個功能可用,但它沒有。 雖然,如果我輸入layout_behavior,它不會抱怨。 也許它的工作......?

無論如何,我已經定義了我自己的自定義布局行為,並且我正在嘗試應用它,但它似乎不起作用。 任何見解將不勝感激。

這是布局。 我正在嘗試將自定義行為應用於第一個LinearLayout(search_polls_toolbar),並在垂直recyclerview向上滾動時向上滾動。 (就像工具欄目前所做的那樣。)我還應該提一下,這個xml用於viewpager中的片段。 托管活動附加了一個協調器布局,使工具欄向上滾動。 (因為那會有沖突嗎?)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/root"
    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">
<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:id="@+id/search_polls_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        android:background="@color/icitizen_toolbar_orange"
        android:weightSum="1"
      app:layout_behavior="com.example.chrisjohnson.icitizenv2.CustomBehaviors.ToolbarBehavior"
        >

        <EditText
            android:id="@+id/search_polls"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/search_polls"
            android:gravity="center_horizontal"
            android:layout_weight=".5"
            android:drawableLeft="@drawable/magnifying_glass"
            android:drawableStart="@drawable/magnifying_glass"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="15dp"
            android:drawablePadding="-50dp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:paddingBottom="10dp"
            android:cursorVisible="false"
            android:textSize="20sp"
            android:background="@color/icitizen_light_orange"
            />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/poll_horizontal_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_below="@id/search_polls_toolbar"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:scrollbars="none"
        >

    </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/poll_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_below="@id/poll_horizontal_recycler_view"
        app:layout_scrollFlags="scroll|enterAlways"
        android:scrollbars="vertical" />

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/polls_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/white_plus_icon"
    android:layout_marginBottom="70dp"
    app:backgroundTint="@color/icitizen_orange"
    app:layout_anchor="@id/container"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:layout_marginRight="15dp"
    android:layout_marginEnd="15dp"/>

這是自定義行為:

public class ToolbarBehavior extends CoordinatorLayout.Behavior<Toolbar> {
    public ToolbarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        Toast.makeText(context, "AJSJA", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {
        return dependency instanceof RecyclerView;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, Toolbar child, View dependency) {
        child.setTranslationY(child.getY());
        return true;
    }
}

這是托管活動的布局。

    <?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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
</android.support.design.widget.CoordinatorLayout>

如果有人希望我發布我的代碼,請告訴我! 謝謝 :)

它不起作用的原因是具有Behavior視圖必須是CoordinatorLayout的直接子項。 在您的情況下,層次結構是: CoordinatorLayout - > RelativeLayout - > LinearLayout (帶Behavior )。

我有這樣的布局。 上部區域有一些東西,但底部只包含一個深層嵌套的FAB。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/your_coordinator_id">

    <android.support.constraint.ConstraintLayout
        app:layout_behavior="com.yourpackage.YourBehavior">

        <ScrollView>
            ...
        </ScrollView>

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

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

        <!--
            Everything "around" the FAB needs to be moved.
        -->
        <RelativeLayout
            android:id="@+id/your_view_id">

           <com.github.jorgecastilloprz.FABProgressCircle>

                <!--
                   This is the actual FAB.
                -->
                <android.support.design.widget.FloatingActionButton/>
            </com.github.jorgecastilloprz.FABProgressCircle>
        </RelativeLayout>
    </android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>

FAB深深嵌套。

CoordinatorLayout > ConstraintLayout > RelativeLayout > FABProgressCircle > FAB

但是,當顯示Snackbar時,需要通過CoordinatorLayout推高RelativeLayout

執行此操作的行為如下所示。

package com.yourpackage;

...

public class YourBehavior extends CoordinatorLayout.Behavior<ConstraintLayout> {

    public YourBehavior(Context context, AttributeSet attrs) {
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        // Note that the RelativeLayout gets translated.
        child.findViewById(R.id.your_view_id).setTranslationY(translationY);
        return true;
    }

    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
        child.findViewById(R.id.your_view_id).setTranslationY(0.0f);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, ConstraintLayout child, View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;
    }
}

像這樣顯示Snackbar

Snackbar.make(findViewById(R.id.your_coordinator_id), "Message", Snackbar.LENGTH_SHORT).show();

onDependentViewRemoved需要被覆蓋,因為當手動關閉SnackbarCoordinatorLayout不會觸發將已翻譯的ViewFloatingActionButton及其RelativeLayout )移回其原始位置。 覆蓋該方法我們可以將其轉換回原來的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM