AppBarLayout.ScrollingViewBehavior-屏幕底部视图底部 [英] AppBarLayout.ScrollingViewBehavior - bottom of view off screen

查看:84
本文介绍了AppBarLayout.ScrollingViewBehavior-屏幕底部视图底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 AppBarLayout 与标准 ScrollingViewBehavior 一起使用时,默认情况下,AppBarLayout的兄弟姐妹为CoordinatorLayout的高度,而兄弟姐妹的底部在屏幕上的高度会偏离屏幕的高度.AppBarLayout.

When using an AppBarLayout with the standard ScrollingViewBehavior, the AppBarLayout's sibling will by default be the height of the CoordinatorLayout and the sibling's bottom will be offscreen by the height of the AppBarLayout.

在我的用例中, NestedScrollView 只是允许工具栏折叠的一种工具,同时在可折叠工具栏下方显示另一个可滚动视图(在这种情况下为碎片).片段是包含固定视图的片段(在这种情况下为FAB)

In my use case, the NestedScrollView is merely a vehicle to allow for the collapsing of the toolbar, while displaying another scrollable view (fragment in this case) beneath the collapsible toolbar. The fragment is the one who contains the bottom-pinned view (FAB in this case)

下面的图片演示了我正在描述的问题,提供的代码是导致问题的基本XML.

Pictures below demonstrate the issue I am describing, and the code supplied is the basic XML which causes the issue.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

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

        <LinearLayout
            android:id="@+id/fragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>

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

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

推荐答案

我发现的此问题的解决方案包括2个部分.

The solution I found to this issue involves 2 parts.

  1. NestedScrollView 的底部添加等于 AppBarLayout 高度的填充.就我而言,因为AppBarLayout仅包含 Toolbar ,所以高度为?attr/actionBarSize .
    android:paddingBottom =?attr/actionBarSize"

  1. Add padding equal to the height of the AppBarLayout to the BOTTOM of the NestedScrollView. In my case because the AppBarLayout only contained a Toolbar, the height was ?attr/actionBarSize.
    android:paddingBottom="?attr/actionBarSize"

AppBarLayout 中添加自定义 AppBarLayout.OnOffsetChangedListener ,这会在折叠工具栏时更改 NestedScrollView 的高度.

Adding a custom AppBarLayout.OnOffsetChangedListener to the AppBarLayout which changes the height of the NestedScrollView as the toolbar is collapsed.

class ScrollingOffsetFixListener(
    private val nestedScrollView: NestedScrollView
): AppBarLayout.OnOffsetChangedListener {

private var originalHeight = 0
private var firstOffset = true

override fun onOffsetChanged(layout: AppBarLayout?, offset: Int) {
    if(firstOffset) {
        firstOffset = false
        originalHeight = nestedScrollView.measuredHeight
    }

    val params = nestedScrollView.layoutParams
    params.height = originalHeight + (offset * -1)

    nestedScrollView.layoutParams = params
   }
}

这篇关于AppBarLayout.ScrollingViewBehavior-屏幕底部视图底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆