【问题标题】:CollapsingToolbarLayout and NestedScrollView not workingCollapsingToolbarLayout 和 NestedScrollView 不起作用
【发布时间】:2015-09-22 06:18:17
【问题描述】:

我正在尝试使用 NestedScrollView 实现 CollapsingToolbarLayout,它在底部的 NestedScrollView 中显示 TextView,并且不允许、滚动或折叠工具栏。我已经让它与 RecyclerView 一起工作,但不是 NestedScrollView。当我删除 app:layout_behavior="@string/appbar_scrolling_view_behavior 时,工具栏会折叠,但 NestedScrollView 不在 AppBarLayout 下方。有什么解决方案或建议来解决这个问题?

XML

<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:fitsSystemWindows="true">

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Hello"
                android:textColor="#000"
                android:textSize="16sp"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="134dp"
                android:background="@color/primary"
                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"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

结果

【问题讨论】:

  • 在 CoordinatorLayout 和 NestedScrollView 中添加 android:fitsSystemWindows="true"。接下来将 nestedScrollview 移到 AppBarLayout 上方。
  • @ɥʇᴉɾuɐɹ 刚刚用您的要求更新了我的答案。结果还是一样。
  • @ɥʇᴉɾuɐɹ 如果你有一个简单的工作示例,发布它,我会测试它。
  • 在 AppbarLayout 中更改 android:layout_height="300dp"。是的,我之前曾参考过一个项目,让我搜索一下。同时试试这个
  • @ɥʇᴉɾuɐɹ 试过了,还是不行。那个参考有运气吗?

标签: android android-design-library androiddesignsupport android-collapsingtoolbarlayout


【解决方案1】:

我必须添加 CoordinatorLayout 才能使其工作:

<androidx.coordinatorlayout.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"
    xmlns:tools="http://schemas.android.com/tools">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="136dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <!--Your layout here-->

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

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

        <!--And here-->

    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

PS:随着android支持库的变化 androidx.coordinatorlayout.widget.CoordinatorLayoutandroid.support.design.widget.CoordinatorLayout

androidx.core.widget.NestedScrollViewandroid.support.v4.widget.NestedScrollView

【讨论】:

    【解决方案2】:

    我遇到了同样的问题并为此编写了一种快速修复程序。假设您的 LinearLayout 包含“Hello”标签绑定到 mContainer 并且您的 Toolbar 绑定到 mToolbar 您可以使用这个:

    private void fixNestedScrollViewScrolling() {
        final int bottomMargin = getScreenHeight() - mContainer.getHeight() - mToolbar.getHeight();
        final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                mContainer.getLayoutParams());
        layoutParams.setMargins(0, 0, 0, bottomMargin);
        mContainer.setLayoutParams(layoutParams);
    }
    
    private int getScreenHeight() {
        final Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        return size.y;
    }
    

    这个修复方法是在我的ActivityonCreate方法中调用的:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_post);
    
        fixNestedScrollViewScrolling();
    
        // some other stuff here ...
    }
    

    【讨论】:

      【解决方案3】:

      在 AppbarLayout 中更改为某个特定高度。示例:

      android:layout_height="300dp". 
      

      主要问题是,嵌套滚动视图没有足够的视图来导致滚动。因此视差效果不起作用。

      Here is a working example that uses NestedScrollView and CollapsingToolbarLayout

      【讨论】:

      • 我不需要在我的 NestedScrollView 上设置特定大小,只需将高度设置为 match_parent 而不是 wrap_content,我就能够在滚动视图中没有任何视图的情况下折叠 AppBarLayout。跨度>
      猜你喜欢
      • 2015-11-11
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 2016-11-08
      • 2017-06-01
      相关资源
      最近更新 更多