【问题标题】:how to do smooth scrolling with nested scroll view inside coordinator layout如何在协调器布局中使用嵌套滚动视图进行平滑滚动
【发布时间】:2018-04-05 03:41:09
【问题描述】:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main_appbar"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main_collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="">

            <fragment xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.mydermacy.www.beyou.activities.CompareClinicsActivity" />

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

        </android.support.design.widget.CollapsingToolbarLayout>
    </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">


    <android.support.v7.widget.RecyclerView
            android:id="@+id/rc_clinic_compare"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_overlapTop="184dp"
            android:background="@color/background" />

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

    <!--app:layout_behavior="@string/appbar_scrolling_view_behavior" />-->

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

我有CoordinatorLayout 作为根视图的布局,在里面我有两个子视图AppBarLayoutNestedScrollView。我无法进行平滑滚动。怎样做才能实现平滑滚动?

【问题讨论】:

  • 我认为您不需要将 RecyclerView 放入 NestedScrollView。它还可能阻止 RecyclerView “回收”。那应该是您的问题的根源。
  • 如果我删除了嵌套的滚动视图,那么回收视图将位于顶部,实际上我希望它就像用户向上滚动卡片时一样,然后地图片段视图将被隐藏并显示标题,这就是我拥有的原因使用了 appbarlayout,我必须为此使用嵌套视图。
  • 尝试使用 canScrollVertically() 返回 false 来制作自己的 LayoutManager,如下所示:stackoverflow.com/a/41134806/2900210

标签: android


【解决方案1】:

CoordinatorLayoutCollapsingToolbarLayout 平滑滚动是bug,Google 仍然没有修复它。 :|

删除NestedScrollViewRecyclerViewapp:layout_behavior="@string/appbar_scrolling_view_behavior" 够了,然后修复。

您可以使用第三方库:smooth-app-bar-layout

【讨论】:

  • 这正是我要寻找的。我还没有尝试过,但看起来很完美。谢谢。点赞?
  • 对我不起作用,但使用它有帮助。在我的 recyclerview 上设置以禁用嵌套滚动。 recyclerView.setNestedScrollingEnabled(false);
【解决方案2】:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/main_appbar"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main_collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:title="">

        <fragment xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.mydermacy.www.beyou.activities.CompareClinicsActivity" />

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

    </android.support.design.widget.CollapsingToolbarLayout>
</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">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rc_clinic_compare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_overlapTop="184dp"
        android:background="@color/background" />

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

<!--app:layout_behavior="@string/appbar_scrolling_view_behavior" />-->
</RelativeLayout>
</ScrollView>

【讨论】:

    【解决方案3】:

    我从回收视图中删除了嵌套滚动视图和 app:behavior_overlapTop="184dp" 所以问题也得到了解决我添加了 app:layout_behavior="@string/appbar_scrolling_view_behavior" 到回收视图感谢帮助

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多