问题
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.
Add padding equal to the height of the
AppBarLayoutto the BOTTOM of theNestedScrollView. In my case because the AppBarLayout only contained aToolbar, the height was?attr/actionBarSize.android:paddingBottom="?attr/actionBarSize"Adding a custom
AppBarLayout.OnOffsetChangedListenerto theAppBarLayoutwhich changes the height of theNestedScrollViewas 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