自定义协调器布局行为+嵌套滚动

时间:2018-05-16 07:59:39

标签: android android-coordinatorlayout android-nestedscrollview

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

  <ui.home.navigation.NavigationView
      android:id="@+id/navigation_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:orientation="horizontal"
      app:layout_behavior =".ui.behavior.NavigationViewBehavior"
      />

  <android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
</android.support.design.widget.CoordinatorLayout>

ViewPager包含多个片段并具有NestedScrollViews。 我希望滚动scrollView时NavigationView的高度会缩小。 我已经添加了自定义行为:

package ui.behavior

import android.content.Context
import android.support.design.widget.CoordinatorLayout
import android.support.v4.widget.NestedScrollView
import android.util.AttributeSet
import android.util.Log
import android.view.View

class NavigationViewBehavior(
  context: Context,
  attributeSet: AttributeSet
) : CoordinatorLayout.Behavior<NavigationView>(context, attributeSet) {

  override fun layoutDependsOn(
    parent: CoordinatorLayout?,
    child: NavigationView?,
    dependency: View?
  ): Boolean {
    Log.d("Behavior", "Dependency = ${dependency.toString()}")
    return dependency is NestedScrollView
  }

  override fun onDependentViewChanged(
    parent: CoordinatorLayout?,
    child: NavigationView?,
    dependency: View?
  ): Boolean {
    if (child is NavigationView && dependency is NestedScrollView) {
      // Adjust navigation view according to scroll Y value
    }
    return false
  }
}

但onDependentViewChanged永远不会被触发,因为只有ViewPager被传递到layoutDependsOn。

如何响应嵌套滚动视图的滚动事件?

0 个答案:

没有答案