blob: b017bb44d7512b29b6a1f437f168a39cec178636 [file] [log] [blame]
arangelov38a6fce2019-12-02 18:21:22 +00001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.app;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22
23import com.android.internal.widget.ViewPager;
24
25/**
26 * A {@link ViewPager} which wraps around its first child's height.
27 * <p>Normally {@link ViewPager} instances expand their height to cover all remaining space in
28 * the layout.
29 * <p>This class is used for the intent resolver picker's tabbed view to maintain
30 * consistency with the previous behavior.
31 */
32public class WrapHeightViewPager extends ViewPager {
33
34 public WrapHeightViewPager(Context context) {
35 super(context);
36 }
37
38 public WrapHeightViewPager(Context context, AttributeSet attrs) {
39 super(context, attrs);
40 }
41
42 public WrapHeightViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
43 super(context, attrs, defStyleAttr);
44 }
45
46 public WrapHeightViewPager(Context context, AttributeSet attrs,
47 int defStyleAttr, int defStyleRes) {
48 super(context, attrs, defStyleAttr, defStyleRes);
49 }
50
51 // TODO(arangelov): When we have multiple pages, the height should wrap to the currently
52 // displayed page. Investigate whether onMeasure is called when changing a page, and instead
53 // of getChildAt(0), use the currently displayed one.
54 @Override
55 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
56 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
57 if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.AT_MOST) {
58 return;
59 }
60 widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
61 int height = getMeasuredHeight();
62 if (getChildCount() > 0) {
63 View firstChild = getChildAt(0);
64 firstChild.measure(widthMeasureSpec,
65 MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
66 height = firstChild.getMeasuredHeight();
67 }
68 heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
69 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
70 }
71}