协调器布局自定义布局行为永远不会被调用 [英] Coordinator layout custom layout behavior never getting called

查看:26
本文介绍了协调器布局自定义布局行为永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想先介绍一下我对协调器布局的了解不足.我只是在学习我在网上找到的教程,并且很好奇为什么我的行为不起作用.

协调器布局内的子视图必须是应用栏布局吗?或者你可以在里面放任何视图.

此外,当我定义 res-auto 命名空间时,它没有为我提供 layout_behavior 的选项.通常,如果某个功能可用而它没有,android studio 会自动完成.虽然,如果我输入 layout_behavior 它不会抱怨.所以也许它正在工作......?

无论如何,我已经定义了我自己的自定义布局行为并且正在尝试应用它,但它似乎不起作用.任何见解将不胜感激.

这是布局.我正在尝试将我的自定义行为应用于第一个 LinearLayout (search_polls_toolbar),并在垂直回收视图向上滚动时让它向上滚动.(就像当前的工具栏一样.)我还应该提到,这个 xml 是用于 viewpager 中的一个片段.并且托管活动附加了一个协调器布局,它确实使工具栏向上滚动.(会不会因此而发生冲突?)

<相对布局android:id="@+id/容器"android:layout_width="match_parent"android:layout_height="match_parent"机器人:方向=垂直"><线性布局android:id="@+id/search_polls_toolbar"android:layout_width="match_parent"android:layout_height="?android:actionBarSize"android:background="@color/icitizen_toolbar_orange"机器人:weightSum =1"app:layout_behavior="com.example.chrisjohnson.icitizenv2.CustomBehaviors.ToolbarBehavior"><编辑文本android:id="@+id/search_polls"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="@string/search_polls"机器人:重力=center_horizo​​ntal"机器人:layout_weight=".5"android:drawableLeft="@drawable/magnifying_glass"android:drawableStart="@drawable/magnifying_glass"android:layout_marginTop="5dp"android:layout_marginLeft="15dp"android:drawablePadding="-50dp"机器人:paddingLeft =5dp"android:paddingTop="5dp"机器人:paddingBottom =10dp"机器人:cursorVisible =假"android:textSize="20sp"android:background="@color/icitizen_light_orange"/></LinearLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/poll_horizo​​ntal_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.support.v7.widget.RecyclerView><android.support.v7.widget.RecyclerViewandroid:id="@+id/poll_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"android:layout_below="@id/poll_horizo​​ntal_recycler_view"app:layout_scrollFlags="scroll|enterAlways"机器人:滚动条=垂直"/></RelativeLayout><android.support.design.widget.FloatingActionButtonandroid:id="@+id/polls_fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/white_plus_icon"机器人:layout_marginBottom="70dp"app:backgroundTint="@color/icitizen_orange"应用程序:layout_anchor="@id/container"应用程序:layout_anchorGravity="底部|右|端"应用程序:borderWidth="0dp"机器人:layout_marginRight="15dp"android:layout_marginEnd="15dp"/>

这是自定义行为:

public class ToolbarBehavior extends CoordinatorLayout.Behavior{公共工具栏行为(上下文上下文,属性集属性){超级(上下文,属性);Toast.makeText(context, "AJSJA", Toast.LENGTH_LONG).show();}@覆盖public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {返回 RecyclerView 的依赖实例;}@覆盖公共布尔 onDependentViewChanged(CoordinatorLayout 父级,工具栏子级,视图依赖项){child.setTranslationY(child.getY());返回真;}}

这是主办活动的布局.

 <?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.AppBarLayoutandroid:id="@+id/appBarLayout"android:layout_width="match_parent"android:layout_height="wrap_content"><android.support.v7.widget.Toolbarandroid:id="@+id/工具栏"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.ViewPagerandroid: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).

Firstly, I'd like to preface this with my lack of knowledge about the coordinator layout. I'm merely following tutorials I found online and am curious why my behavior isn't working.

Does the child view inside of coordinator layout have to be app bar layout? Or are you able to put any view inside there.

Also, when I define the res-auto namespace it doesn't give me the option for layout_behavior. Usually android studio will auto-complete if a function is available and it didn't. Although, if I type out layout_behavior it doesn't complain. So maybe it's working...?

Regardless, I've defined my own custom layout behavior and am trying to apply it but it doesn't seem to be working. Any insight would be greatly appreciated.

Here is the layout. I'm trying to apply my custom behavior to the first LinearLayout (search_polls_toolbar) and have it scroll up when the vertical recyclerview scrolls up. (Like the toolbar currently does.) I should also mention, this xml is for a fragment in a viewpager. And the hosting activity has a coordinator layout attached to it that does make the toolbar scroll up. (Could it be conflicted because of that?)

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

And here's the custom behavior:

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;
    }
}

And here's the hosting activies' layout.

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

If anyone would like me to post anymore of my code, please let me know! Thanks :)

解决方案

The reason why it is not working is that view with Behavior must be a direct child of CoordinatorLayout. In your case, the hierarchy is: CoordinatorLayout -> RelativeLayout -> LinearLayout (with Behavior).

这篇关于协调器布局自定义布局行为永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆