AppBarLayout.ScrollingViewBehavior - bottom of view off screen

人走茶凉 提交于 2021-01-27 15:40:43

问题


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.

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)

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>


回答1:


The solution I found to this issue involves 2 parts.

  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"

  2. 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
       }
    }
    


来源:https://stackoverflow.com/questions/53549168/appbarlayout-scrollingviewbehavior-bottom-of-view-off-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!