【问题标题】:CoordinatorLayout.Behavior causes onStartNestedScroll(..) twiceCoordinatorLayout.Behavior 导致 onStartNestedScroll(..) 两次
【发布时间】:2017-05-25 07:58:31
【问题描述】:

我有一个问题。

我在里面使用带有 NestedScrollView 的 CoordinatorLayout。 NestedScrollView 包含 TextView + RecyclerView。我也有带有附加行为的 AppBarLayout。

当我与 RecyclerView 交互时,行为正常。但是如果我尝试与 TextView 交互,行为会导致 onStartNestedScroll(..) 然后 onStopNestedScroll(..) 和 onStartNestedScroll(..) 再次发生。

为什么会这样?如何预防?

<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">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/aspot"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:background="#39868686"
                android:focusableInTouchMode="true"
                android:padding="100dp"
                android:text="Some text"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:orientation="vertical"
             app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

        </LinearLayout>

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@color/transparent"
        app:elevation="0dp"
        app:layout_behavior="com.example.coordinatorbehavior.ScrollingBehavior">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="AppBarLayout"/>
    </android.support.design.widget.AppBarLayout>

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

ScrollingBehavior 非常简单。

public class ScrollingBehavior extends CoordinatorLayout.Behavior<AppBarLayout> {

public ScrollingBehavior() {
}

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

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
    boolean started = nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    Log.d("log", "onStartNestedScroll: " + started);
    return started;
}

@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) {
    Log.d("log", "onStopNestedScroll");
    super.onStopNestedScroll(coordinatorLayout, child, target);
}

}

【问题讨论】:

标签: java android android-coordinatorlayout


【解决方案1】:

将属性app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到NestedScrollView

如下更新您的布局:

<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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@color/transparent"
        app:elevation="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="AppBarLayout"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/aspot"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:background="#39868686"
                android:focusableInTouchMode="true"
                android:padding="100dp"
                android:text="Some text"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="false"
                android:orientation="vertical"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

        </LinearLayout>

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

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

【讨论】:

  • 这不是我的决定,因为我使用自定义行为。我对 AppBarLayout 有不同的行为。 dropbox.com/s/ukvuln3e0u01doc/device-2017-05-25-140824.mp4?dl=0 如您所见,当我从标题滚动时出现故障,因为在 onNested(..) 事件中有一些间隙
  • 您是否在自己的布局中尝试过 app:layout_behavior="@string/appbar_scrolling_view_behavior" 到 NestedScrollView?
猜你喜欢
  • 2015-02-18
  • 1970-01-01
  • 2013-12-25
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 2018-07-26
  • 2013-04-18
相关资源
最近更新 更多