我正在尝试使用 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;
}
这将发送滚动事件,并且您将适本地调用 layoutDependsOn
和 onDependentViewChanged
。
关于android - 未调用具有自定义行为的 CoordinatorLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192454/