1 package com.stoutner.privacybrowser.definitions;
4 * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
6 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
8 * Privacy Browser is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Privacy Browser is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
22 import android.content.Context;
23 import android.support.v4.view.ViewPager;
24 import android.util.AttributeSet;
25 import android.view.View;
27 public class WrapVerticalContentViewPager extends ViewPager {
28 // Setup the default constructors.
29 public WrapVerticalContentViewPager(Context context) {
33 public WrapVerticalContentViewPager(Context context, AttributeSet attrs) {
34 super(context, attrs);
38 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
39 // Perform an initial `super.onMeasure`, which populates `getChildCount`.
40 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
42 // Initialize `maximumHeight`.
43 int maximumHeight = 0;
45 // Find the maximum height of each of the child views.
46 for (int i = 0; i < getChildCount(); i++) {
47 View childView = getChildAt(i);
49 // Measure the child view height with no constraints.
50 childView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
52 // Store the child's height if it is larger than `maximumHeight`.
53 if (childView.getMeasuredHeight() > maximumHeight) {
54 maximumHeight = childView.getMeasuredHeight();
58 // Perform a final `super.onMeasure` to set the `maximumHeight`.
59 super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(maximumHeight, MeasureSpec.EXACTLY));