blob: 014df9ca27a67adf04bedc156863fe6965721134 [file] [log] [blame]
Justin Klaassen4b3af052014-05-27 17:53:10 -07001/*
2 * Copyright (C) 2014 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.calculator2;
18
19import android.content.Context;
20import android.support.v4.view.PagerAdapter;
21import android.support.v4.view.ViewPager;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.ViewGroup;
25
26public class CalculatorPadViewPager extends ViewPager {
27
28 private final PagerAdapter mStaticPagerAdapter = new PagerAdapter() {
29 @Override
30 public int getCount() {
31 return getChildCount();
32 }
33
34 @Override
35 public Object instantiateItem(ViewGroup container, int position) {
36 return getChildAt(position);
37 }
38
39 @Override
40 public void destroyItem(ViewGroup container, int position, Object object) {
41 removeViewAt(position);
42 }
43
44 @Override
45 public boolean isViewFromObject(View view, Object object) {
46 return view == object;
47 }
48
49 @Override
50 public float getPageWidth(int position) {
Justin Klaassen0587d222014-06-19 10:44:21 -070051 return position == 1 ? 7.0f / 9.0f : 1.0f;
Justin Klaassen4b3af052014-05-27 17:53:10 -070052 }
53 };
Justin Klaassen5c324202014-06-10 20:27:20 -070054
55 private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {
56 private void recursivelySetEnabled(View view, boolean enabled) {
57 if (view instanceof ViewGroup) {
58 final ViewGroup viewGroup = (ViewGroup) view;
59 for (int childIndex = 0; childIndex < viewGroup.getChildCount(); ++childIndex) {
60 recursivelySetEnabled(viewGroup.getChildAt(childIndex), enabled);
61 }
62 } else {
63 view.setEnabled(enabled);
64 }
65 }
66
67 @Override
68 public void onPageSelected(int position) {
69 if (getAdapter() == mStaticPagerAdapter) {
70 for (int childIndex = 0; childIndex < getChildCount(); ++childIndex) {
71 // Only enable subviews of the current page.
72 recursivelySetEnabled(getChildAt(childIndex), childIndex == position);
73 }
74 }
75 }
76 };
77
Justin Klaassen4b3af052014-05-27 17:53:10 -070078 private final PageTransformer mPageTransformer = new PageTransformer() {
79 @Override
80 public void transformPage(View view, float position) {
Justin Klaassen5c324202014-06-10 20:27:20 -070081 if (position < 0.0f) {
Justin Klaassen4b3af052014-05-27 17:53:10 -070082 // Pin the left page to the left side.
83 view.setTranslationX(getWidth() * -position);
Justin Klaassen5c324202014-06-10 20:27:20 -070084 view.setAlpha(Math.max(1.0f + position, 0.0f));
Justin Klaassen4b3af052014-05-27 17:53:10 -070085 } else {
Justin Klaassen5c324202014-06-10 20:27:20 -070086 // Use the default slide transition when moving to the next page.
87 view.setTranslationX(0.0f);
88 view.setAlpha(1.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -070089 }
90 }
91 };
92
93 public CalculatorPadViewPager(Context context) {
94 this(context, null);
95 }
96
97 public CalculatorPadViewPager(Context context, AttributeSet attrs) {
98 super(context, attrs);
99
100 setAdapter(mStaticPagerAdapter);
Justin Klaassen5c324202014-06-10 20:27:20 -0700101 setBackgroundColor(getResources().getColor(android.R.color.black));
102 setOnPageChangeListener(mOnPageChangeListener);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700103 setPageMargin(getResources().getDimensionPixelSize(R.dimen.pad_page_margin));
104 setPageTransformer(false, mPageTransformer);
105 }
106
107 @Override
108 protected void onFinishInflate() {
109 super.onFinishInflate();
110
111 // Invalidate the adapter's data set since children may have been added during inflation.
112 if (getAdapter() == mStaticPagerAdapter) {
113 mStaticPagerAdapter.notifyDataSetChanged();
114 }
115 }
116}