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
--- 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);
}
}