Bug 1337933 - Make the tabs grid layout decorations work with RTL. r=maliu a=gchang
authorTom Klein <twointofive@gmail.com>
Thu, 06 Apr 2017 19:52:10 -0500
changeset 395842 7c1dafe56c49e662c7c0f3380bf20d853b6b4fd2
parent 395841 4c78da5f6ddacce5332ead124aa734635fc83d8f
child 395843 da3f4f145b694d6fe9aa1ddea82ef4c4a3f2ada4
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmaliu, gchang
bugs1337933
milestone54.0a2
Bug 1337933 - Make the tabs grid layout decorations work with RTL. r=maliu a=gchang This patch affects all grided tabs panel layouts: tablets, phones in landscape mode, and phones in portrait mode with the "Compact tabs" setting on. MozReview-Commit-ID: 5cqVJA57ARu
mobile/android/base/java/org/mozilla/gecko/widget/GridSpacingDecoration.java
--- a/mobile/android/base/java/org/mozilla/gecko/widget/GridSpacingDecoration.java
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/GridSpacingDecoration.java
@@ -3,16 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.widget;
 
 import android.graphics.Rect;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.ViewUtils;
 import android.view.View;
 
 /**
  * An ItemDecoration for a GridLayoutManager that provides fixed spacing (but not fixed padding)
  * to create fixed sized items, with no spacing on the outer edges of the outer items.
  * <p>
  * So, for example, if there are 2 columns and the spacing is s, then the first column gets a right
  * padding of s/2 and the second column gets a left paddding of s/2.  If there are three columns
@@ -33,16 +34,19 @@ public class GridSpacingDecoration exten
     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
         final int position = parent.getChildAdapterPosition(view);
         if (position == RecyclerView.NO_POSITION) {
             return;
         }
 
         final GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
         final int spanCount = layoutManager.getSpanCount();
-        final int column = position % spanCount;
+        // If we're RTL then column counts start from the right, but view is still LTR (i.e. offsets
+        // are still LTR), so compute offsets using the LTR column position (also note that this
+        // only works because offsets in a given column are the same for LTR and RTL layouts).
+        final int LTRColumn = ViewUtils.isLayoutRtl(parent) ? (spanCount - 1) - (position % spanCount) : position % spanCount;
 
-        final int columnLeftOffset = (int) (((float) column / (float) spanCount) * horizontalSpacing);
-        final int columnRightOffset = (int) (((float) (spanCount - (column + 1)) / (float) spanCount) * horizontalSpacing);
+        final int columnLeftOffset = (int) (((float) LTRColumn / (float) spanCount) * horizontalSpacing);
+        final int columnRightOffset = (int) (((float) (spanCount - (LTRColumn + 1)) / (float) spanCount) * horizontalSpacing);
 
         outRect.set(columnLeftOffset, verticalPadding, columnRightOffset, verticalPadding);
     }
 }