Android rtl viewpager with rtl tablayout

≡放荡痞女 提交于 2020-01-10 06:08:09

问题


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?


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/46828728/android-rtl-viewpager-with-rtl-tablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!