Fix PagerIndicatorView disabled dot in RTL languages
This commit is contained in:
parent
2386684b7f
commit
700d8de5be
|
@ -23,6 +23,7 @@ public class PagerIndicatorView extends View {
|
||||||
private int disabledPage = -1;
|
private int disabledPage = -1;
|
||||||
private int circleColor = 0;
|
private int circleColor = 0;
|
||||||
private int circleColorHighlight = -1;
|
private int circleColorHighlight = -1;
|
||||||
|
private boolean isLocaleRtl = false;
|
||||||
|
|
||||||
public PagerIndicatorView(Context context) {
|
public PagerIndicatorView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
@ -40,6 +41,9 @@ public class PagerIndicatorView extends View {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setup() {
|
private void setup() {
|
||||||
|
isLocaleRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
|
||||||
|
== ViewCompat.LAYOUT_DIRECTION_RTL;
|
||||||
|
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
paint.setStyle(Paint.Style.FILL);
|
paint.setStyle(Paint.Style.FILL);
|
||||||
|
|
||||||
|
@ -50,6 +54,14 @@ public class PagerIndicatorView extends View {
|
||||||
a.recycle();
|
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) {
|
public void setViewPager(ViewPager2 pager) {
|
||||||
numPages = pager.getAdapter().getItemCount();
|
numPages = pager.getAdapter().getItemCount();
|
||||||
pager.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
pager.getAdapter().registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
||||||
|
@ -59,22 +71,18 @@ public class PagerIndicatorView extends View {
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
boolean isLocaleRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
|
|
||||||
== ViewCompat.LAYOUT_DIRECTION_RTL;
|
|
||||||
pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||||
PagerIndicatorView.this.position = position + positionOffset;
|
PagerIndicatorView.this.position = logicalPositionToVisual(
|
||||||
if (isLocaleRtl) {
|
position + positionOffset);
|
||||||
PagerIndicatorView.this.position = numPages - 1 - PagerIndicatorView.this.position;
|
|
||||||
}
|
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDisabledPage(int disabledPage) {
|
public void setDisabledPage(int disabledPage) {
|
||||||
this.disabledPage = disabledPage;
|
this.disabledPage = (int) logicalPositionToVisual(disabledPage);
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue