I spent a lot of time searching and applying the proposed answers, but I did not succeed, so I decided to delve deeper into the problem and find the full answer that I want to share here. Maybe this will help someone.
So the problem is that when the ViewPager is used with a collapsing toolbar , the former does not calculate its height properly. And if you want the ViewPager to fill all the space to the bottom of the screen, but no more - there is a problem. Just adding layout_marginBottom will not solve the problem, because the Collapsing Toolbar will change its height when the user scrolls.
Therefore, if you want your ViewPager to adapt its height to fit the height of the folding toolbar , you need 2 things:
- Custom scroll behavior.
- GlobalLayoutListener, which will correct the height of the ViewPager on first drawing.
Here they are (written in Kotlin, but this is just a separate file and can be placed directly in a Java project):
import android.app.Activity import android.content.Context import android.graphics.Point import android.support.design.widget.AppBarLayout import android.support.design.widget.CoordinatorLayout import android.util.AttributeSet import android.view.View import android.view.ViewGroup import android.view.ViewTreeObserver /** * Custom extension of AppBarLayout.ScrollingViewBehavior to deal with ViewPager height * in a bunch with Collapsing Toolbar Layout. Works dynamically when AppBar Layout height is changing. */ class ViewPagerScrollingBehavior(context: Context, attributeSet: AttributeSet? = null) : AppBarLayout.ScrollingViewBehavior(context, attributeSet) { override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean { val layoutParams = child.layoutParams as CoordinatorLayout.LayoutParams layoutParams.height = child.height - (dependency.bottom - child.top) child.layoutParams = layoutParams child.requestLayout() return super.onDependentViewChanged(parent, child, dependency) } } /** * Custom implementation of ViewTreeObserver.OnGlobalLayoutListener to fix the View height * in a bunch with Collapsing Toolbar Layout. Works when View is drawn on the screen for first time. * To be used with ViewPagerScrollingBehavior. */ class FixHeightGlobalLayoutListener(val activity: Activity, val view: View) : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { val display = activity.windowManager.defaultDisplay val size = Point() display.getSize(size) val height = size.y val location = IntArray(2) view.getLocationOnScreen(location) view.post { val layoutParams = view.layoutParams as ViewGroup.LayoutParams layoutParams.height = height - location[1] view.layoutParams = layoutParams view.requestLayout() } view.viewTreeObserver.removeOnGlobalLayoutListener(this) } }
And use them in your code:
Add custom behavior to your ViewPager : app:layout_behavior="your.package.ViewPagerScrollingBehavior"
Add a custom OnGlobalLayoutListener to your ViewPager : viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new FixHeightGlobalLayoutListener(this, viewPager));
source share