CoordinatorLayout:onDependentViewChanged未被调用

时间:2017-10-27 08:28:28

标签: android

我有一个非常简单的方案涉及NestedScrollViewLinearLayout,我希望LinearLayoutNestedScrollView向上滚动时翻译。以下是图片中的布局:(相关的子视图是第二个LinearLayout,其ID为linearlayout

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

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.snapsboardmainpage.MainActivity"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="vertical"
                android:layout_gravity="top"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="0dp"
                android:layout_marginRight="0dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginLeft="0dp"
                app:layout_constraintLeft_toLeftOf="parent">

            </LinearLayout>


            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="0dp"
                android:layout_height="112dp"
                android:orientation="vertical"
                android:background="@android:color/holo_green_light"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/linearLayout3">

            </LinearLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/id_viewpager_photosvideos_albums"
                android:layout_width="0dp"
                android:layout_height="1000dp"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout1">

                <android.support.design.widget.TabLayout
                    android:id="@+id/id_tab_photosvideos_albums"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top">

                    <android.support.design.widget.TabItem
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <android.support.design.widget.TabItem
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

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


            </android.support.v4.view.ViewPager>


        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="vertical"
        android:layout_gravity="top"
        app:layout_behavior="com.example.snaps.TopActionBarBehavior"
        android:background="@android:color/holo_blue_light">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:orientation="vertical"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_orange_light">

    </LinearLayout>

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

并且,有关行为(TopActionBarBehavior)如下:

public class TopActionBarBehavior extends CoordinatorLayout.Behavior<LinearLayout> {

    private static final String TAG = "TopActionBarBehavior";
    private int thresholdScrollDistance;
    private int mNestedScrollViewInitialTop;

    public TopActionBarBehavior() {
    }

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

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {

        // Translate the child view as per the NestedScrollView
        int currentNestedScrollViewTop = dependency.getTop();

        // Have we scrolled up?
        if(currentNestedScrollViewTop > mNestedScrollViewInitialTop){
            // Translate the child view by the same distance
            child.setTranslationY(currentNestedScrollViewTop - mNestedScrollViewInitialTop);
        }

        return true;
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, LinearLayout child, int layoutDirection) {

        View topBar = parent.findViewById(R.id.linearLayout);
        int topBarHeight = topBar.getHeight();

        View nestedScrollView = parent.findViewById(R.id.nested_container);
        mNestedScrollViewInitialTop = nestedScrollView.getTop();

        View profileBar = parent.findViewById(R.id.linearLayout1);
        int profileBarHeight = profileBar.getHeight();

        View dummyTopBarUnderlay = parent.findViewById(R.id.linearLayout3);
        int dummyTopBarHeight = dummyTopBarUnderlay.getHeight();

        View tabLayout = parent.findViewById(R.id.id_tab_photosvideos_albums);
        int tabLayoutHeight = tabLayout.getHeight();

        thresholdScrollDistance = profileBarHeight + dummyTopBarHeight + tabLayoutHeight;

        return super.onLayoutChild(parent, child, layoutDirection);
    }

}

我尝试过调试,而layoutDependsOn()正在被调用,但onDependentViewChanged()只被调用一次。

1 个答案:

答案 0 :(得分:0)

经过无数小时的调试后终于找到了答案。我的NestedScrollView实际上根本没有移动;它的内容在整个时间都在移动,并没有触发这种方法。对于滚动事件,我们可以使用onNested*()类中的Behavior方法。简单。