Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuPagerAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuPagerAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuPagerAdapter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..41ea222a40b58a365b5792088e99515c7680dbf7 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuPagerAdapter.java |
| @@ -0,0 +1,71 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.contextmenu; |
| + |
| +import android.support.v4.text.TextUtilsCompat; |
| +import android.support.v4.view.PagerAdapter; |
| +import android.support.v4.view.ViewCompat; |
| +import android.util.Pair; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| + |
| +import java.util.List; |
| +import java.util.Locale; |
| + |
| +/** |
| + * Takes a list of views and strings and creates a wrapper for the ViewPager and Tab adapter. |
| + */ |
| +class TabularContextMenuPagerAdapter extends PagerAdapter { |
| + private final List<Pair<String, ViewGroup>> mViewList; |
| + private final boolean mIsRightToLeft; |
| + |
| + /** |
| + * Used in combination of a TabLayout to create a multi view layout. |
| + * @param views Thew views to use in the pager Adapter. |
| + */ |
| + TabularContextMenuPagerAdapter(List<Pair<String, ViewGroup>> views) { |
| + mViewList = views; |
| + mIsRightToLeft = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) |
| + == ViewCompat.LAYOUT_DIRECTION_RTL; |
| + } |
| + |
| + // Used thanks due to a bug in displaying RTL. |
|
David Trainor- moved to gerrit
2017/03/24 17:24:20
// Addresses bug RTL display bug?
|
| + private int adjustIndexForDirectionality(int index, int count) { |
| + if (mIsRightToLeft) { |
| + return count - 1 - index; |
| + } |
| + return index; |
| + } |
| + |
| + @Override |
| + public Object instantiateItem(ViewGroup container, int position) { |
| + position = adjustIndexForDirectionality(position, getCount()); |
| + ViewGroup layout = mViewList.get(position).second; |
| + container.addView(layout); |
| + return layout; |
| + } |
| + |
| + @Override |
| + public void destroyItem(ViewGroup container, int position, Object object) { |
| + position = adjustIndexForDirectionality(position, getCount()); |
| + container.removeViewAt(position); |
| + } |
| + |
| + @Override |
| + public int getCount() { |
| + return mViewList.size(); |
| + } |
| + |
| + @Override |
| + public boolean isViewFromObject(View view, Object object) { |
| + return view == object; |
| + } |
| + |
| + @Override |
| + public CharSequence getPageTitle(int position) { |
| + position = adjustIndexForDirectionality(position, getCount()); |
| + return mViewList.get(position).first; |
| + } |
| +} |