Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2014 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.support.v4.view.ViewPager;
     21 
     22 import com.android.mail.utils.ViewUtils;
     23 
     24 /**
     25  * Helper class for adding RTL support to a ViewPager. Note that this also requires a PagerAdapter
     26  * that returns the pages in reversed order in the RTL case.
     27  */
     28 public class BidiViewPagerHelper {
     29     private ViewPager mViewPager;
     30 
     31     public BidiViewPagerHelper(ViewPager viewPager) {
     32         mViewPager = viewPager;
     33     }
     34 
     35     public int getFirstPage() {
     36         return getBidiIndex(0);
     37     }
     38 
     39     public int getLastPage() {
     40         return getBidiIndex(mViewPager.getAdapter().getCount() - 1);
     41     }
     42 
     43     public int getPreviousPage() {
     44         return clampToBounds(getBidiIndex(mViewPager.getCurrentItem() - 1));
     45     }
     46 
     47     public int getNextPage() {
     48         return clampToBounds(getBidiIndex(mViewPager.getCurrentItem() + 1));
     49     }
     50 
     51     private int clampToBounds(int position) {
     52         return Math.max(0, Math.min(mViewPager.getAdapter().getCount() - 1, position));
     53     }
     54 
     55     private int getBidiIndex(int position) {
     56         if (ViewUtils.isViewRtl(mViewPager)) {
     57             return mViewPager.getAdapter().getCount() - 1 - position;
     58         } else {
     59             return position;
     60         }
     61     }
     62 }