Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rtl support to rectangle indicator #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
rectIndicatorButton.setOnClickListener(v -> {
activity.addFragment(new RectIndicatorFragment());
});

Button rectIndicatorRtlButton = view.findViewById(R.id.button_main_rect_indicator_rtl);
rectIndicatorRtlButton.setOnClickListener(v -> {
activity.addFragment(new RectIndicatorRtlFragment());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.vejei.viewpagerindicator.sample;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import io.github.vejei.viewpagerindicator.indicator.RectIndicator;

public class RectIndicatorRtlFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_rect_indecator_rtl, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

MainActivity activity = (MainActivity) getActivity();
if (activity == null) return;

PageAdapter pageAdapter = new PageAdapter();
ViewPager2 viewPager2 = view.findViewById(R.id.view_pager2);
viewPager2.setAdapter(pageAdapter);
pageAdapter.setData(Arrays.asList(activity.data));

RectIndicator rectIndicator = view.findViewById(R.id.rect_indicator);
rectIndicator.setWithViewPager2(viewPager2);

RectIndicator squareIndicator = view.findViewById(R.id.square_indicator);
squareIndicator.setWithViewPager2(viewPager2);

RectIndicator stadiumIndicator = view.findViewById(R.id.stadium_indicator);
stadiumIndicator.setWithViewPager2(viewPager2);

List<String> newData = Arrays.asList("Seventh", "Eighth", "Ninth");
pageAdapter.append(newData);
}
}
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
android:layout_height="wrap_content"
android:text="@string/main_rect_indicator_button_text"/>

<Button
android:id="@+id/button_main_rect_indicator_rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_rect_indicator_rtl_button_text"/>

</LinearLayout>
65 changes: 65 additions & 0 deletions sample/src/main/res/layout/fragment_rect_indecator_rtl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".CircleIndicatorFragment"
android:layoutDirection="rtl">

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_height="200dp"/>

<io.github.vejei.viewpagerindicator.indicator.RectIndicator
android:id="@+id/rect_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_pager2"
app:indicatorColor="@android:color/darker_gray"
app:indicatorSelectedColor="@color/colorAccent"
app:indicatorGap="8dp"
app:indicatorWidth="20dp"
app:indicatorHeight="5dp"
app:indicatorCornerRadius="0dp" />

<io.github.vejei.viewpagerindicator.indicator.RectIndicator
android:id="@+id/square_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/rect_indicator"
app:indicatorColor="@android:color/darker_gray"
app:indicatorSelectedColor="@color/colorAccent"
app:indicatorGap="8dp"
app:indicatorWidth="10dp"
app:indicatorHeight="10dp"
app:indicatorCornerRadius="0dp" />

<io.github.vejei.viewpagerindicator.indicator.RectIndicator
android:id="@+id/stadium_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/square_indicator"
app:indicatorColor="@android:color/darker_gray"
app:indicatorSelectedColor="@color/colorAccent"
app:indicatorGap="8dp"
app:indicatorWidth="20dp"
app:indicatorHeight="5dp"
app:indicatorCornerRadius="10dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">ViewPagerIndicator</string>
<string name="main_circle_indicator_button_text">Circle Indicator</string>
<string name="main_rect_indicator_button_text">Rect Indicator</string>
<string name="main_rect_indicator_rtl_button_text">Rect Indicator Rtl</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ protected int measureHeight() {
@Override
public int getCoordinateX(int position) {
int coordinate = getPaddingLeft();
if(isRtl()) coordinate = getMeasuredWidth() - indicatorWidth;

for (int i = 0; i < getItemCount(); i++) {
if (i == position) {
return coordinate;
}
coordinate += indicatorWidth + indicatorGap;
if (i == position) return coordinate;

if(isRtl()) coordinate -= indicatorWidth + indicatorGap;
else coordinate += indicatorWidth + indicatorGap;

}
return coordinate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ protected void onPageScrolled(int position, float positionOffset) {
}
}

protected boolean isRtl() {
return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
public int getCoordinateX(int position) {
return 0;
}
Expand All @@ -292,4 +295,5 @@ public int getCoordinateY() {

abstract protected int measureWidth();
abstract protected int measureHeight();

}