Skip to content

Commit

Permalink
[MaterialDividerItemDecoration] Add RTL support
Browse files Browse the repository at this point in the history
Resolves #3494

GIT_ORIGIN_REV_ID=774ebec04ab0105c0e2a55890863405f0edfd768
PiperOrigin-RevId: 553822845
  • Loading branch information
pubiqq authored and dsn5ft committed Aug 7, 2023
1 parent 5617e7b commit 6b897c6
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import androidx.annotation.Px;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;

/**
Expand Down Expand Up @@ -318,7 +318,7 @@ private void drawForVerticalOrientation(@NonNull Canvas canvas, @NonNull Recycle
left = 0;
right = parent.getWidth();
}
boolean isRtl = ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL;
boolean isRtl = ViewUtils.isLayoutRtl(parent);
left += isRtl ? insetEnd : insetStart;
right -= isRtl ? insetStart : insetEnd;

Expand Down Expand Up @@ -357,14 +357,25 @@ private void drawForHorizontalOrientation(@NonNull Canvas canvas, @NonNull Recyc
top += insetStart;
bottom -= insetEnd;

boolean isRtl = ViewUtils.isLayoutRtl(parent);

int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (shouldDrawDivider(parent, child)) {
parent.getLayoutManager().getDecoratedBoundsWithMargins(child, tempRect);
// Take into consideration any translationX added to the view.
int right = tempRect.right + Math.round(child.getTranslationX());
int left = right - thickness;
int translationX = Math.round(child.getTranslationX());
int left;
int right;
if (isRtl) {
left = tempRect.left + translationX;
right = left + thickness;
} else {
right = tempRect.right + translationX;
left = right - thickness;
}

dividerDrawable.setBounds(left, top, right, bottom);
dividerDrawable.draw(canvas);
}
Expand All @@ -384,7 +395,11 @@ public void getItemOffsets(
if (orientation == VERTICAL) {
outRect.bottom = thickness;
} else {
outRect.right = thickness;
if (ViewUtils.isLayoutRtl(parent)) {
outRect.left = thickness;
} else {
outRect.right = thickness;
}
}
}
}
Expand Down

0 comments on commit 6b897c6

Please sign in to comment.