2016-12-16 28 views
1

我想實現CoordinatorLayout與RecyclerView和TextView其中TextView將根據您如何滾動RecyclerView動畫。但是我的自定義行爲中的onDependentViewChanged僅在我的視圖第一次膨脹並且在此之後不被調用時才被調用幾次,儘管我滾動了RecyclerView。CoordinatorLayout自定義行爲不叫

我的行爲:

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> 

回答

3

這個問題的答案是,你需要重寫下面的方法在您的自定義行爲類返回true:

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

這將發送滾動事件通過,你會得到呼叫layoutDependsOnonDependentViewChanged