summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/livepicker/widget/ConstraintViewPager.java
blob: 985cad64d018fe0e6b5b4533dc456aac6b6f76c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wallpaper.livepicker.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.android.wallpaper.livepicker.R;

/**
 * When ConstraintViewPager is being measured, it will get all height of pages and makes itself
 * height as the same as the maximum height.
 */
public class ConstraintViewPager extends ViewPager {

    private final int mExtraSpacerHeight;

    public ConstraintViewPager(@NonNull Context context) {
        this(context, null /* attrs */);
    }

    public ConstraintViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mExtraSpacerHeight = context.getResources().getDimensionPixelSize(
                R.dimen.preview_attribution_pane_extra_spacer_height);
    }

    /**
     * Visit all child views first and then determine the maximum height for ViewPager.
     * Info page will add extra height in top area determined by empty space.
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxChildHeight = 0;
        int infoChildHeight = 0;
        int infoTopPadding = 0;
        View infoPage = null;
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            view.measure(widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0 /* size */, MeasureSpec.UNSPECIFIED));
            int childHeight = view.getMeasuredHeight();
            // Get info page height and top padding.
            if (view.getId() == R.id.page_info) {
                infoPage = view;
                infoChildHeight = childHeight;
                infoTopPadding = view.getPaddingTop();
            }
            if (childHeight > maxChildHeight) {
                maxChildHeight = childHeight;
            }
        }

        // Add extra padding in info page top area if info page has enough empty space to
        // accommodate above and below extra height.
        // 1. "infoChildHeight - infoTopPadding" means info page height without extra padding.
        // 2. "mExtraSpacerHeight * 2" means above and below extra height.
        if (maxChildHeight > (infoChildHeight - infoTopPadding + mExtraSpacerHeight * 2)) {
            if (infoPage != null && infoTopPadding != mExtraSpacerHeight) {
                infoPage.setPadding(infoPage.getPaddingLeft(), mExtraSpacerHeight,
                        infoPage.getPaddingRight(), infoPage.getPaddingBottom());
            }
        }

        if (maxChildHeight != 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxChildHeight, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}