使用NestedScrollView的可折叠工具栏布局无法平滑滚动

3
我有一个带有CollapsingToolbarLayout的NestedScrollView,并且我希望它能够平稳地运行。我的问题是,当从内容向上滚动时,我的Collapsing Toolbar不会自动展开,而是在到达顶部时被阻止。然后我需要再次向上滚动才能展开工具栏。我希望实现从内容向上平稳滚动,可以自动展开我的CollapsingToolbarLayout。以下是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/product_detail_main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:apptools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="true"
            android:foregroundGravity="bottom|right"
            android:foregroundTintMode="add"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        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="wrap_content"
        android:orientation="vertical">

       /*
           CONTENT
       */

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

我找到了一些类似的问题,但是没有一个答案对我有用。

尝试在CoordinatorLayout中设置android:fitsSystemWindows="true" - Jay Rathod
没有,不起作用。 - Meemaw
你找到任何解决方案了吗? - amodkanthe
3个回答

3

LinearLayout里面有什么样的内容?

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

   /*
       CONTENT
   */

</LinearLayout>

如果其中一个内容是recyclerview,导致折叠式工具栏不响应滚动的原因是因为NestedScrollView在接收到来自recyclerview的滚动事件时没有调用AppBarLayout.Behavour类。也就是说,当recyclerview发生滚动时,它会将滚动进度传递给NestedScrollView。NestedScrollView接收到滚动事件但不做任何处理。
在NestedScrollView类内部:
@overide
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){
         //Do nothing
}

为了克服这个问题,使得当滚动recyclerview时appbarlayout能够展开/折叠,只需创建一个扩展NestedScrollView的自定义类,并覆盖上述方法并调用dispatchNestedPreScroll()方法,该方法会通知appbarlayout有滚动事件发生并使其响应该事件。
  public class CustomNestedScrollView extends NestedScrollView{

    public CustomNestedScrollView(Context context) {
    super(context);
    }

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

    public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    }

  @overide
  public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){
    dispatchNestedPreScroll(dx,dy,consumed,null);
  }
}

然后在您的layout.xml中使用这个类。
<com.my.package.CustomNestedScrollView
    android:id="@+id/scroll"
    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="wrap_content"
    android:orientation="vertical">

   /*
       CONTENT
   */

</LinearLayout>


@MitulVarmora,您可以提供您的布局,以便我们知道问题出在哪里。 - Edijae Crusar

0

我通过使用SmoothAppBarLayout解决了那个问题
链接: https://github.com/henrytao-me/smooth-app-bar-layout

在你的应用级build.gradle中添加这个依赖

compile "me.henrytao:smooth-app-bar-layout:25.3.1.0"

请注意,NestedScrollView的子项应该具有可点击和可聚焦的属性。
这是XML布局。

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>
    <variable .../>
</data>

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

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayoutPlaceDetail"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/adViewPlaceDetail"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <me.henrytao.smoothappbarlayout.SmoothAppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <!--app:layout_scrollInterpolator="@android:anim/accelerate_decelerate_interpolator"-->

                <ImageView
                    android:id="@+id/imgOfPlace"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:adjustViewBounds="true"
                    android:contentDescription="@string/app_name"
                    android:scaleType="fitXY"
                    android:src="@drawable/placeholder"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways" />

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

        </me.henrytao.smoothappbarlayout.SmoothAppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:overScrollMode="never"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintLayoutInNestedScroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true">

                <YOUR CONTENT VIEWS>

            </android.support.constraint.ConstraintLayout>

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

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

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adViewPlaceDetail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="..."
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>


0
.java 类中设置 RecyclerView 适配器后,添加以下代码:
ViewCompat.setNestedScrollingEnabled(View-name, false);

你应该解释得更多一些,并且格式化你的代码,谢谢。 - Muhammad Dyas Yaskur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接