Home | History | Annotate | Download | only in calculator2
      1 /*
      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 
     17 package com.android.calculator2;
     18 
     19 import android.content.Context;
     20 import android.support.v4.view.PagerAdapter;
     21 import android.support.v4.view.ViewPager;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 
     26 public 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) {
     51             return position == 1 ? 7.0f / 9.0f : 1.0f;
     52         }
     53     };
     54 
     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 
     78     private final PageTransformer mPageTransformer = new PageTransformer() {
     79         @Override
     80         public void transformPage(View view, float position) {
     81             if (position < 0.0f) {
     82                 // Pin the left page to the left side.
     83                 view.setTranslationX(getWidth() * -position);
     84                 view.setAlpha(Math.max(1.0f + position, 0.0f));
     85             } else {
     86                 // Use the default slide transition when moving to the next page.
     87                 view.setTranslationX(0.0f);
     88                 view.setAlpha(1.0f);
     89             }
     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);
    101         setBackgroundColor(getResources().getColor(android.R.color.black));
    102         setOnPageChangeListener(mOnPageChangeListener);
    103         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 }
    117