1 /* 2 * Copyright (C) 2015 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.deskclock.widget; 18 19 import android.content.Context; 20 import android.support.v4.view.ViewPager; 21 import android.text.TextUtils; 22 import android.util.AttributeSet; 23 import android.view.View; 24 25 import java.util.Locale; 26 27 /** 28 * A {@link ViewPager} that's aware of RTL changes when used with FragmentPagerAdapter. 29 */ 30 public final class RtlViewPager extends ViewPager { 31 32 /** 33 * Callback interface for responding to changing state of the selected page. 34 * Positions supplied will always be the logical position in the adapter - 35 * that is, the 0 index corresponds to the left-most page in LTR and the 36 * right-most page in RTL. 37 */ 38 private OnPageChangeListener mListener; 39 RtlViewPager(Context context)40 public RtlViewPager(Context context) { 41 this(context, null /* attrs */); 42 } 43 RtlViewPager(Context context, AttributeSet attrs)44 public RtlViewPager(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 47 @Override 48 public void onPageScrolled(int position, float offset, int offsetPixels) { 49 // Do nothing 50 } 51 52 @Override 53 public void onPageSelected(int position) { 54 if (mListener != null) { 55 mListener.onPageSelected(getRtlAwareIndex(position)); 56 } 57 } 58 59 @Override 60 public void onPageScrollStateChanged(int state) { 61 // Do nothing 62 } 63 }); 64 } 65 66 @Override getCurrentItem()67 public int getCurrentItem() { 68 return getRtlAwareIndex(super.getCurrentItem()); 69 } 70 71 @Override setCurrentItem(int item)72 public void setCurrentItem(int item) { 73 super.setCurrentItem(getRtlAwareIndex(item)); 74 } 75 76 @Override setOnPageChangeListener(OnPageChangeListener unused)77 public void setOnPageChangeListener(OnPageChangeListener unused) { 78 throw new UnsupportedOperationException("Use setOnRTLPageChangeListener instead"); 79 } 80 81 /** 82 * Get a "RTL friendly" index. If the locale is LTR, the index is returned as is. 83 * Otherwise it's transformed so view pager can render views using the new index for RTL. For 84 * example, the second view will be rendered to the left of first view. 85 * 86 * @param index The logical index. 87 */ getRtlAwareIndex(int index)88 public int getRtlAwareIndex(int index) { 89 if (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == 90 View.LAYOUT_DIRECTION_RTL) { 91 return getAdapter().getCount() - index - 1; 92 } 93 return index; 94 } 95 96 /** 97 * Sets a {@link OnPageChangeListener}. The listener will be called when a page is selected. 98 */ setOnRTLPageChangeListener(OnPageChangeListener listener)99 public void setOnRTLPageChangeListener(OnPageChangeListener listener) { 100 mListener = listener; 101 } 102 } 103