我在我的应用程序中使用ViewPager并且我想支持RTL,但是当我将我的设备语言改为RTl时,它似乎确实无法正常工作。
我希望标签从右侧开始(右侧的第一个片段)。
请帮我解决。
编辑:以下是它的图片: Tabs start from the left side
这是我的代码:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/app_bar_main"
android:id="@+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
android:layout_below="@id/toolbar_actionbar">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/fragment_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</com.google.android.gms.ads.AdView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java:
package ...;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme_NoActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle(getString(R.string.app_name));
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Intro(), getString(R.string.Introduction));
adapter.addFragment(new S1(), getString(R.string.stage1));
adapter.addFragment(new S2(), getString(R.string.stage2));
adapter.addFragment(new S3(), getString(R.string.stage3));
adapter.addFragment(new S4(), getString(R.string.stage4));
adapter.addFragment(new S5(), getString(R.string.stage5));
adapter.addFragment(new S6(), getString(R.string.stage6));
adapter.addFragment(new S7(), getString(R.string.stage7));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_contact_us: {
Intent browserIntent1 = new Intent(Intent.ACTION_VIEW,
Uri.parse(getString(R.string.Contact_us_URL)));
startActivity(browserIntent1);
return true;
}
case R.id.action_share: {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = getString(R.string.shareText);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));
return true;
}
case R.id.action_rate_us: {
Intent browserIntent2 = new Intent(Intent.ACTION_VIEW,
Uri.parse(getString(R.string.Rate_us_URL)));
startActivity(browserIntent2);
return true;
}
case R.id.action_help: {
final Intent intent1 = new Intent(MainActivity.this, HelpActivity.class);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent1);
}
});
}
else {
startActivity(intent1);
}
return true;
}
case R.id.action_about_us: {
final Intent intent2 = new Intent(MainActivity.this, AboutActivity.class);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent2);
}
});
}
else {
startActivity(intent2);
}
return true;
}
case R.id.action_settings: {
Intent intent3 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent3);
return true;
}
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.action_contact_us:
Intent browserIntent1 = new Intent(Intent.ACTION_VIEW,
Uri.parse(getString(R.string.Contact_us_URL)));
startActivity(browserIntent1);
break;
case R.id.action_share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = getString(R.string.shareText);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));
break;
case R.id.action_rate_us:
Intent browserIntent2 = new Intent(Intent.ACTION_VIEW,
Uri.parse(getString(R.string.Rate_us_URL)));
startActivity(browserIntent2);
break;
case R.id.action_help:
final Intent intent1 = new Intent(MainActivity.this, HelpActivity.class);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent1);
}
});
}
else {
startActivity(intent1);
}
break;
case R.id.action_about_us:
final Intent intent2 = new Intent(MainActivity.this, AboutActivity.class);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent2);
}
});
}
else {
startActivity(intent2);
}
break;
case R.id.action_settings:
Intent intent3 = new Intent(this, SettingsActivity.class);
startActivity(intent3);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
谢谢!!!
答案 0 :(得分:14)
经过大量研究后,我意识到该怎么做。
我需要做的是扭转片段的顺序
(将它们初始化为ViewPager时)并执行viewPager.setCurrentItem(ViewPagerSize)
。
但只有当语言环境是RTL时才必须发生,所以我使用了一种方法来判断它是不是。
这是代码:
public static boolean isRTL() {
return isRTL(Locale.getDefault());
}
public static boolean isRTL(Locale locale) {
final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
if (isRTL()) {
// The view has RTL layout
adapter.addFragment(new S7(), getString(R.string.stage7));
adapter.addFragment(new S6(), getString(R.string.stage6));
adapter.addFragment(new S5(), getString(R.string.stage5));
adapter.addFragment(new S4(), getString(R.string.stage4));
adapter.addFragment(new S3(), getString(R.string.stage3));
adapter.addFragment(new S2(), getString(R.string.stage2));
adapter.addFragment(new S1(), getString(R.string.stage1));
adapter.addFragment(new Intro(), getString(R.string.Introduction));
} else {
// The view has LTR layout
adapter.addFragment(new Intro(), getString(R.string.Introduction));
adapter.addFragment(new S1(), getString(R.string.stage1));
adapter.addFragment(new S2(), getString(R.string.stage2));
adapter.addFragment(new S3(), getString(R.string.stage3));
adapter.addFragment(new S4(), getString(R.string.stage4));
adapter.addFragment(new S5(), getString(R.string.stage5));
adapter.addFragment(new S6(), getString(R.string.stage6));
adapter.addFragment(new S7(), getString(R.string.stage7));
}
viewPager.setAdapter(adapter);
}
对于选项卡,我必须将方向设置为LTR(当它是RTL时看起来很乱)。
所以我使用了这段代码(它仅在API 17 +中提供):
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
tabLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
我无法在API 17之前找到这样做的方法。
答案 1 :(得分:12)
这个图书馆 RtlViewPager干得好干净
只需要替换
<android.support.v4.view.ViewPager
与
<com.duolingo.open:rtl-viewpager
XML中的
它还支持tabLayout
使用这种方法更简洁,因为它不需要反转适配器逻辑
另请参阅此谷歌的issue报告
答案 2 :(得分:5)
我的解决方案是添加android:rotationY="@integer/view_pager_rotation"
属性到xml中的viewPager
。
values/integers
包含 <integer name="view_pager_rotation">0</integer>
values-ldrtl/integers
包含 <integer name="view_pager_rotation">180</integer>
注意内容也会被轮换。
现在支持viewpager2,更多信息here
答案 3 :(得分:1)
在你给我们的截图中,不仅ViewPager没有绘制它的RTL,也没有任何东西已经是RTL。怎么可能如果你在应用程序标签中设置supportsRtl =&#34; true&#34; ??您必须在“开发”选项下的手机“设置”中设置RTL。如果我没有错,你必须在ViewPager中自动设置任何不自动支持RTL 将ViewPager固定调整为RTL后,您可以设置您想要成为默认片段的片段,将以下内容放在onCreate()中:
viewPager.setCurrentItem(number)
其中number表示基于零的索引中的片段数。例如,要选择第一个,请为第二个集合设置0,依此类推。
答案 4 :(得分:1)
还有另一种简便的方法是将旋转应用于ViewPager
及其子视图,如下所示:
步骤1 。更改查看分页器的旋转方式
if(LocaleUtil.isRTL()) viewPager.setRotationY(180);
第2步。然后再次更改片段容器的方向,该片段容器是ViewPager
的子容器,
if(LocaleUtil.isRTL()) rootView.setRotationY(180);
在我的情况下,我还有另一个片段作为视图分页器的子级,因此在放大布局后,我更改了rootView的方向。
LocaleUtil.java
public class LocaleUtil {
// other functions
public static boolean isRTL() {
return isRTL(Locale.getDefault());
}
private static boolean isRTL(Locale locale) {
final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}
}
答案 5 :(得分:0)
我认为您可以使用viewPager.setCurrentItem()
将当前页面设置为最后一页。然后它会从左向右滑动.. :))
答案 6 :(得分:0)
使用此库
https://github.com/duolingo/rtl-viewpager
implementation 'com.android.support:support-core-ui:<choose_your_version>'
implementation 'com.duolingo.open:rtl-viewpager:1.0.3'
答案 7 :(得分:0)
经过长期研究,我找到了一个非常简单的解决方案。没有反向适配器,类中的旋转子项等等。
open class RtlViewPager : ViewPager {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
override fun onRtlPropertiesChanged(layoutDirection: Int) {
super.onRtlPropertiesChanged(layoutDirection)
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
rotationY = 180f
}
}
override fun onViewAdded(child: View?) {
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
child?.rotationY = 180f
}
super.onViewAdded(child)
}
}
答案 8 :(得分:0)
不需要任何第三方库。 Android最近添加了名为 ViewPager2 的新UI组件,该组件支持RTL布局。
https://developer.android.com/jetpack/androidx/releases/viewpager2
请在下面找到答案的链接。答案讨论了迁移指南和示例github应用程序以支持RTL视图分页器。
答案 9 :(得分:0)
对我有用:
ViewCompat.setLayoutDirection(binding.pager, ViewCompat.LAYOUT_DIRECTION_RTL);