android - 使用 NestedScrollView 折叠工具栏布局不会平滑滚动

标签 android android-layout android-nestedscrollview

我有一个带有 CollapsingToolbarLayoutNestedScrollView,我想让它顺利运行。

我的问题是,当从内容向上滚动时,我的折叠工具栏不会自动展开,而是在到达顶部时被阻止。然后我需要再次向上滚动以展开工具栏。

我想实现从自动展开我的 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>

我发现了一些类似的问题,但没有一个答案对我有用。

最佳答案

LinearLayout 里面有什么样的内容?

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

   /*
       CONTENT
   */

</LinearLayout>

如果其中一个内容是recyclerview,collapsing toolbar不响应滚动的原因是NestedScrollView没有调用AppBarLayout.Behavour 当它从 recyclerview 接收到滚动事件时类。也就是说,当在 recyclerview 中发生滚动时,recyclerview 将滚动进度分派(dispatch)/传递给 NestedScrollViewNestedScrollView 接收滚动事件但不对其执行任何操作。

在 nestedscrollview 类中

@overide
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){
         //Do nothing
}

为了克服这个问题并使 appbarlayout 在滚动 recyclerview 时展开/折叠,只需创建一个自定义类来扩展 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>

关于android - 使用 NestedScrollView 折叠工具栏布局不会平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981994/

相关文章:

android - Retrofit 返回空数组列表,但给出了有效的 POJO 和 Json 来处理(尝试了其他问题,但对它们起作用的答案不在 mne 上)

android - Poputaing 时 ListView 为空

android - 在 Android PreferenceActivity 中更改分隔线颜色

Android:解析包含 Html 实体、Html 字符和 URL 地址的 XML 文件?

android - 检查布局是否包含任何 ImageView 或 TextView android

android - 为什么 motionEvent.getX() 在使用 setScaleX() 时返回不一致的值?

android - NestedScrollView 内的 RecyclerView 中的 SmoothScrollTo 项目

android - 如何在空的edittext上更改 Material TextInputLayout提示颜色?

android - 当 recyclerview 放置在 scrollview/nestedScrollView 中时,scrollToPosition 方法不起作用

android - NestedScrollview 内的 RecyclerView 滚动不流畅