Bug 1337933 - Make the tabs grid layout decorations work with RTL. r?maliu draft
authorTom Klein <twointofive@gmail.com>
Thu, 06 Apr 2017 19:52:10 -0500
changeset 557672 e330c88f7f378973a9b0bf1c6b7abc11aab2222a
parent 556469 867df9483d5af4c8c12e19fab9b0de18bee30db7
child 623111 e7261e38c487de6c982c68e54f04a484f9afe1dc
push id52779
push userbmo:twointofive@gmail.com
push dateFri, 07 Apr 2017 04:19:59 +0000
reviewersmaliu
bugs1337933
milestone55.0a1
Bug 1337933 - Make the tabs grid layout decorations work with RTL. r?maliu 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);
     }
 }