OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.contextmenu; |
| 6 |
| 7 import android.support.v4.text.TextUtilsCompat; |
| 8 import android.support.v4.view.PagerAdapter; |
| 9 import android.support.v4.view.ViewCompat; |
| 10 import android.util.Pair; |
| 11 import android.view.View; |
| 12 import android.view.ViewGroup; |
| 13 |
| 14 import java.util.List; |
| 15 import java.util.Locale; |
| 16 |
| 17 /** |
| 18 * Takes a list of views and strings and creates a wrapper for the ViewPager and
Tab adapter. |
| 19 */ |
| 20 class TabularContextMenuPagerAdapter extends PagerAdapter { |
| 21 private final List<Pair<String, ViewGroup>> mViewList; |
| 22 private final boolean mIsRightToLeft; |
| 23 |
| 24 /** |
| 25 * Used in combination of a TabLayout to create a multi view layout. |
| 26 * @param views Thew views to use in the pager Adapter. |
| 27 */ |
| 28 TabularContextMenuPagerAdapter(List<Pair<String, ViewGroup>> views) { |
| 29 mViewList = views; |
| 30 mIsRightToLeft = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.get
Default()) |
| 31 == ViewCompat.LAYOUT_DIRECTION_RTL; |
| 32 } |
| 33 |
| 34 // Addresses the RTL display bug: https://code.google.com/p/android/issues/d
etail?id=56831 |
| 35 private int adjustIndexForDirectionality(int index, int count) { |
| 36 if (mIsRightToLeft) { |
| 37 return count - 1 - index; |
| 38 } |
| 39 return index; |
| 40 } |
| 41 |
| 42 @Override |
| 43 public Object instantiateItem(ViewGroup container, int position) { |
| 44 position = adjustIndexForDirectionality(position, getCount()); |
| 45 ViewGroup layout = mViewList.get(position).second; |
| 46 container.addView(layout); |
| 47 return layout; |
| 48 } |
| 49 |
| 50 @Override |
| 51 public void destroyItem(ViewGroup container, int position, Object object) { |
| 52 position = adjustIndexForDirectionality(position, getCount()); |
| 53 container.removeViewAt(position); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public int getCount() { |
| 58 return mViewList.size(); |
| 59 } |
| 60 |
| 61 @Override |
| 62 public boolean isViewFromObject(View view, Object object) { |
| 63 return view == object; |
| 64 } |
| 65 |
| 66 @Override |
| 67 public CharSequence getPageTitle(int position) { |
| 68 position = adjustIndexForDirectionality(position, getCount()); |
| 69 return mViewList.get(position).first; |
| 70 } |
| 71 } |
OLD | NEW |