Android rtl viewpager with rtl tablayout
Asked Answered
G

3

1

I know this question may seem duplicated but i couldn't find a good solution for my problem. I am using tablayout with viewpager with fragmentPagerAdapter as adapter of viewpager. As you know viewpager is not supporting rtl layout so i have problems in rtl locales. I want to tabs start from right(first tab stick to right side of screen) and user can swipe correctly. How can i do this? Any help?

Genitalia answered 19/10, 2017 at 11:21 Comment(0)
H
4

Good news guys :). Android recently added new UI component called ViewPager2.

https://developer.android.com/jetpack/androidx/releases/viewpager2

Please find below the code and links :

dependencies {
    implementation "androidx.viewpager2:viewpager2:1.0.0"
}

Improvements from the previous ViewPager implementation:

  • RTL (right-to-left) layout support
  • Vertical orientation support
  • Reliable Fragment support (including handling changes to the underlying Fragment collection)
  • Dataset change animations (including DiffUtil support)

Below link explains about Migration guide from ViewPager to ViewPager2:

https://developer.android.com/training/animation/vp2-migration

Also we have demo example :

https://github.com/android/views-widgets-samples/tree/master/ViewPager2

Howenstein answered 2/1, 2020 at 6:2 Comment(0)
B
8

The best solution would be to rotate the viewpager to 180:

mViewPager.setRotationY(180); //rotate the viewpager

then, rotate the inner view that contains the content of the tab for tablayout (to reverse the effect on the content).An example in the case of a textview:

if (textView.getRotationY() != 180) {
    textView.setRotationY(180);
 }

For PagerTabStrip, you should also rotate 180 the 3 visible tabs:

PagerTabStrip pt= container.findViewById(R.id.pager_tab_strip);
 pt.getChildAt(0).setRotationY(180);
 pt.getChildAt(1).setRotationY(180);
 pt.getChildAt(2).setRotationY(180);

That worked for me in an arabic app. It should work also for any rtl locales.

Bobby answered 23/3, 2018 at 17:19 Comment(6)
rotationY = 180 makes a swipe bug in android Pie (level 28)Circus
@do01 why so? What did you do for Android P?Sarpedon
@RushiMThakker yes it don't let swipe normally :(Circus
In Android P there is jerk while swiping the viewpager. Anyone got solution for this ?Fixed
So did you find any solution for Pie?Derek
I have added answerHowenstein
H
4

Good news guys :). Android recently added new UI component called ViewPager2.

https://developer.android.com/jetpack/androidx/releases/viewpager2

Please find below the code and links :

dependencies {
    implementation "androidx.viewpager2:viewpager2:1.0.0"
}

Improvements from the previous ViewPager implementation:

  • RTL (right-to-left) layout support
  • Vertical orientation support
  • Reliable Fragment support (including handling changes to the underlying Fragment collection)
  • Dataset change animations (including DiffUtil support)

Below link explains about Migration guide from ViewPager to ViewPager2:

https://developer.android.com/training/animation/vp2-migration

Also we have demo example :

https://github.com/android/views-widgets-samples/tree/master/ViewPager2

Howenstein answered 2/1, 2020 at 6:2 Comment(0)
G
2

I have faced this case before and there are 2 solutions

1- let us assume that you have 4 fragments F1,F2,F3 and F4, now you have to fetch all fragments in adapter in reversely as this F4,F3,F2 and F1, once you load your fragments set the select fragment is = pager.setCurrentItem(Titles.length - 1);

and make sure to reverse the titles also.

the second solution is to use custom view pager like

RTL view pager

hope you handle this

Gratin answered 19/10, 2017 at 11:31 Comment(1)
Problem with first solution is i want to tabs start from right side in rtl locales but with this solution they starts from end and first tab is fourth from left and problem with second solution is that i cant use fragmentPagerAdapter with this custom viewpagersGenitalia

© 2022 - 2024 — McMap. All rights reserved.