android - 未调用具有自定义行为的 CoordinatorLayout

标签 android android-coordinatorlayout

我正在尝试使用 RecyclerView 和 TextView 实现 CoordinatorLayout,其中 TextView 将根据您滚动 RecyclerView 的方式进行动画处理。但是,尽管我滚动了 RecyclerView,但我的自定义行为中的 onDependentViewChanged 仅在 View 首次膨胀时被调用几次,之后就不再被调用。

我的行为:

public class Behavior extends CoordinatorLayout.Behavior<TextView> {

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

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
        return true;
    }
}

我的 XML:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blahhhhhhhhhhh"
        app:layout_behavior="com.mypackage.Behavior"/>

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

最佳答案

这个问题的答案是您需要重写自定义行为类中的以下方法以返回 true:

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, TextView child, View directTargetChild, View target, int nestedScrollAxes) {
    return true;
}

这将发送滚动事件,并且您将适本地调用 layoutDependsOnonDependentViewChanged

关于android - 未调用具有自定义行为的 CoordinatorLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192454/

相关文章:

android - 布局中的 TabHost NullPointerException

android - 如何在 Android 上将输入事件注入(inject)自己的应用程序?

android - UniversalImageLoader 安卓 : Add white border around rounded image

android - CoordinatorLayout - 朝相反的方向摆动会导致抖动

android - NestedScrollView 内的 webview 导致高度问题

android - 使用 fab 的底部导航

android - 如何在 Android 中禁用 Snackbar 和 FAB 动画的 CoordinatorLayout

android - 使用相机快照防止多项 Activity 堆叠

Android Material Design 个人资料页面

Android布局: Stick one Layout to the top of the screen and place another Layout centred below