【问题标题】:Fragment content is hidden behind BottomNavigationView when scrolling滚动时 Fragment 内容隐藏在 BottomNavigationView 后面
【发布时间】:2017-10-28 17:40:54
【问题描述】:

我已经对此进行了大量研究,但不知道该怎么做 - 我的片段中有一些内容被活动中的底部导航栏剪掉,我不确定该怎么做。我尝试将app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到 NestedScrollView,但内容的底部(位置名称)仍然被截断 - 可能有一个简单的解决方法,但我无法弄清楚。我的主要活动和“主页”片段的 XML 如下:

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/app_bar" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@color/navbarBackground"
        app:menu="@menu/bottom_nav_menu" />
</LinearLayout>

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">

    <ImageView
        android:id="@+id/tokyo_placeholder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitStart"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/main_screen_placeholder" />

    <ImageView
        android:id="@+id/airplane_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_background"
        app:layout_constraintBottom_toBottomOf="@+id/tokyo_placeholder"
        app:layout_constraintLeft_toLeftOf="@+id/tokyo_placeholder"
        app:layout_constraintRight_toRightOf="@+id/tokyo_placeholder"
        app:layout_constraintTop_toTopOf="@+id/tokyo_placeholder"
        app:srcCompat="@drawable/icons8_airplane_48"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="286dp"
        android:layout_marginBottom="@dimen/app_bar_height"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tokyo_placeholder"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:id="@+id/destination_headline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/featured_destinations_headline"
                android:textAllCaps="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                android:textSize="14sp"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/featured_destinations_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/destination_headline" />

            <TextView
                android:id="@+id/saved_trips_headline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/featured_destinations_recycler_view"
                android:fontFamily="sans-serif"
                android:text="@string/saved_trips"
                android:textAllCaps="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                android:textSize="14sp"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/saved_trips_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/saved_trips_headline" />
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:background="#fff"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

作为一个例子来说明我的意思,here is the content when the navigation bar is moved out of the wayhere is the content with the bar in the way。我认为问题是某种边距/填充问题,但无法完全弄清楚要解决的问题。

编辑:我已经创建了一个包含相关代码的存储库(以及一个可行的模拟器示例)here - 请注意代码是用 Kotlin 编写的,但我不认为问题在于任何实际代码,但更多在于布局

【问题讨论】:

  • 你能在 github 上发布一个具有这种行为的简单项目吗?
  • 能否提供@layout/app_bar.xml的代码
  • 你能去掉 CoordinatorLayout n 试试
  • @azizbekian 当然,今晚下班后我会做的!
  • @MiteshVanaliya 完成!

标签: android android-layout android-fragments kotlin android-nestedscrollview


【解决方案1】:

我不确定预期的行为是什么,仅通过阅读您的问题我无法理解。已应用 this patch,其中仅包含 home_fragment.xml 布局更改:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">

    <FrameLayout
        android:id="@+id/header_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/tokyo_placeholder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/main_screen_placeholder"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/airplane_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:background="@drawable/circle_background"
            app:srcCompat="@drawable/icons8_airplane_48"
            tools:ignore="ContentDescription" />
    </FrameLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:paddingLeft="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header_container">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:id="@+id/destination_headline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/featured_destinations_headline"
                android:textAllCaps="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                android:textSize="14sp"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/featured_destinations_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/destination_headline"
                app:layout_constraintBottom_toTopOf="@id/saved_trips_headline"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/destination_headline" />

            <TextView
                android:id="@+id/saved_trips_headline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:text="@string/saved_trips"
                android:textAllCaps="true"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                android:textSize="14sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintBottom_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/featured_destinations_recycler_view" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/saved_trips_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toTopOf="@id/app_bar"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/saved_trips_headline" />
        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

然后你会得到这个输出:

【讨论】:

  • 工作就像一个魅力!非常感谢!
【解决方案2】:

@Tai M. 问题似乎是 NestedScrollView 的 硬编码 高度。由于您已经在使用 ConstraintLayout,因此您可以轻松放置此视图,而无需添加另一个相对布局。

如果您想让标题图像一个在另一个之上,请将它们包装在 FrameLayout 中。为容器分配一个 id 并添加: 应用程序:layout_constraintTop_toBottomOf="@+id/headerImageContainer" 到 NestedScrollView。

然后,您可能可以摆脱 RelativeLayout 并将其替换为另一个 ConstraintLayout 以仅使用约束来构造其余视图。

另外,根据材料设计的定义,BottomNavigationView 的高度是 56dp,您可以直接指定它而不是让它将内容包裹在 activity_home.xml

<ConstraintLayout ...>

    <FrameLayout ...>
        <ImageView .../>

        <ImageView .../>

    </FrameLayout>

    <NestenScrollView ... wrap_content>
        <ConstraintLayout ...>
            <TextView .../>
            <RecyclerView .../>
            <!-- Assign constraints accordingly -->
            <TextView .../>
            <RecyclerView .../>

        </ConstraintLayout>

    </NestenScrollView>

</ConstraintLayout>

希望这会有所帮助。

【讨论】:

  • 只是为了确保我遵循,当你说摆脱 relativelayout 时,你指的是 NestedScrollView 中的那个?如果是这种情况,那是不可能的,因为 NestedScrollViews 只允许有一个直接子级,所以我需要在其中进行某种形式的布局,以免引发错误。
  • @TaiM。我稍微编辑了答案。我试图在评论中添加它,但不能这样做。这应该会有所帮助。
  • 按照你的例子,但现在内容根本不会滚动,recyclerviews 工作,但nestedscrollview 本身将无法正常工作。我想我将使用这部分代码创建一个单独的存储库,以查看是否有人可以重现我正在经历的问题,我觉得这可能比仅使用代码 sn-ps 更容易。
  • 我在原帖中添加了github链接!
  • @TaiM。我检查了你的项目并运行了它。滚动视图按预期工作。虽然没有那么敏感。也许您可以尝试通过启用 featuredDestinationsRecycler.setItemViewCacheSize(10) featuredDestinationsRecycler.isDrawingCacheEnabled = true 来优化您的 recyclerView 实现,并且布局管理器也设置预取:featuredDestinationsLayoutManager.initialPrefetchItemCount = 5 featuredDestinationsLayoutManager.isItemPrefetchEnabled = true 如果有帮助,请点赞。
猜你喜欢
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多