簡體   English   中英

具有自定義行為的CoordinatorLayout不被調用

[英]CoordinatorLayout with custom behavior not called

我正在嘗試使用RecyclerView和TextView實現一個CoordinatorLayout,其中TextView將根據您滾動RecyclerView的方式進行動畫處理。 但是,盡管我滾動了RecyclerView,但自定義行為中的onDependentViewChanged僅在視圖第一次膨脹時才被調用幾次,此后才被調用。

我的行為:

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

暫無
暫無

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

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