Merge pull request #4097 from ebraminio/dots

Fix PagerIndicatorView disabled dot in RTL languages
This commit is contained in:
H. Lehmann 2020-05-03 10:22:07 +02:00 committed by GitHub
commit d95a642fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 7 deletions

View File

@ -23,6 +23,7 @@ public class PagerIndicatorView extends View {
private int disabledPage = -1;
private int circleColor = 0;
private int circleColorHighlight = -1;
private boolean isLocaleRtl = false;
public PagerIndicatorView(Context context) {
super(context);
@ -40,6 +41,9 @@ public class PagerIndicatorView extends View {
}
private void setup() {
isLocaleRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
== ViewCompat.LAYOUT_DIRECTION_RTL;
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
@ -50,6 +54,14 @@ public class PagerIndicatorView extends View {
a.recycle();
}
/**
* Visual and logical position distinction only happens in RTL locales (e.g. Persian)
* where pages positions are flipped thus it does nothing in LTR locales (e.g. English)
*/
private float logicalPositionToVisual(float position) {
return isLocaleRtl ? numPages - 1 - position : position;
}
public void setViewPager(ViewPager2 pager) {
numPages = pager.getAdapter().getItemCount();
pager.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@ -59,22 +71,18 @@ public class PagerIndicatorView extends View {
invalidate();
}
});
boolean isLocaleRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
== ViewCompat.LAYOUT_DIRECTION_RTL;
pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
PagerIndicatorView.this.position = position + positionOffset;
if (isLocaleRtl) {
PagerIndicatorView.this.position = numPages - 1 - PagerIndicatorView.this.position;
}
PagerIndicatorView.this.position = logicalPositionToVisual(
position + positionOffset);
invalidate();
}
});
}
public void setDisabledPage(int disabledPage) {
this.disabledPage = disabledPage;
this.disabledPage = (int) logicalPositionToVisual(disabledPage);
invalidate();
}