如何创建 RTL ViewPager?
How to create RTL ViewPager?
我在我的应用程序中使用 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);
}
}
谢谢!!!
我认为您可以使用 viewPager.setCurrentItem()
将当前页设置为最后一页。然后它将从左向右滑动.. :)
在您给我们的屏幕截图中,不仅 ViewPager 没有绘制其自身的 RTL,而且没有任何东西已经是 RTL。如果您在应用程序标签中设置 supportsRtl="true" 怎么可能?您必须在开发选项下的 phone 设置中设置 RTL。如果我没记错的话,您必须在 ViewPager 中不设置任何内容以自动支持 RTL
在将 ViewPager 适当调整为 RTL 后,您可以设置要将哪个片段作为默认片段,将以下内容放入 onCreate():
viewPager.setCurrentItem(number)
其中数字表示基于零的索引中的片段数。比如给select第一个设0,给第二个设1,以此类推。
经过大量研究,我知道了如何去做。
我需要做的是颠倒片段的顺序
(将它们初始化为 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 之前执行此操作的方法。
这个图书馆
RtlViewPager工作干干净净
只需要更换
<android.support.v4.view.ViewPager
和
<com.duolingo.open:rtl-viewpager
在XML
它也支持tabLayout
使用这种方法更简洁,因为它不需要反转适配器逻辑
另请参阅此 issue 报告 google
我的解决方案是添加 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>
注意内容也会旋转。
2019 年 2 月 11 日更新
现在 viewpager2 支持它。
使用这个库
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'
还有另一种简单的方法是将旋转应用于 ViewPager
及其子视图,如下所示:
步骤 1。更改视图分页器的旋转
if(LocaleUtil.isRTL()) viewPager.setRotationY(180);
步骤 2。然后再次更改作为 ViewPager
、
子项的片段容器的方向
if(LocaleUtil.isRTL()) rootView.setRotationY(180);
在我的例子中,我有另一个 fragment 作为 view pager 的子片段,所以在膨胀布局后我改变了 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;
}
}
更新
您可以使用 ViewPager2,它支持从右到左 (RTL),这将根据应用区域设置自动启用。
经过长时间的研究,我找到了一个非常简单的解决方案。没有反向适配器,旋转 child 在他们的 类 等
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)
}
}
重写为Java:
public class RtlViewPager extends ViewPager {
public RtlViewPager(@NonNull Context context) {
super(context);
}
public RtlViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
setRotationY(180f);
}
}
@Override
public void onViewAdded(View child) {
if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
child.setRotationY(180);
}
super.onViewAdded(child);
}
}
不需要任何第三方库。 Android 最近添加了名为 ViewPager2 的新 UI 组件,它支持 RTL 布局。
https://developer.android.com/jetpack/androidx/releases/viewpager2
请在下面link 中找到答案。答案谈到了迁移指南和示例 github 应用程序以支持 RTL 视图寻呼机。
对我有用:
ViewCompat.setLayoutDirection(binding.pager, ViewCompat.LAYOUT_DIRECTION_RTL);
我在我的应用程序中使用 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);
}
}
谢谢!!!
我认为您可以使用 viewPager.setCurrentItem()
将当前页设置为最后一页。然后它将从左向右滑动.. :)
在您给我们的屏幕截图中,不仅 ViewPager 没有绘制其自身的 RTL,而且没有任何东西已经是 RTL。如果您在应用程序标签中设置 supportsRtl="true" 怎么可能?您必须在开发选项下的 phone 设置中设置 RTL。如果我没记错的话,您必须在 ViewPager 中不设置任何内容以自动支持 RTL 在将 ViewPager 适当调整为 RTL 后,您可以设置要将哪个片段作为默认片段,将以下内容放入 onCreate():
viewPager.setCurrentItem(number)
其中数字表示基于零的索引中的片段数。比如给select第一个设0,给第二个设1,以此类推。
经过大量研究,我知道了如何去做。
我需要做的是颠倒片段的顺序
(将它们初始化为 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 之前执行此操作的方法。
这个图书馆 RtlViewPager工作干干净净
只需要更换
<android.support.v4.view.ViewPager
和
<com.duolingo.open:rtl-viewpager
在XML
它也支持tabLayout
使用这种方法更简洁,因为它不需要反转适配器逻辑
另请参阅此 issue 报告 google
我的解决方案是添加 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>
注意内容也会旋转。
2019 年 2 月 11 日更新
现在 viewpager2 支持它。
使用这个库
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'
还有另一种简单的方法是将旋转应用于 ViewPager
及其子视图,如下所示:
步骤 1。更改视图分页器的旋转
if(LocaleUtil.isRTL()) viewPager.setRotationY(180);
步骤 2。然后再次更改作为 ViewPager
、
if(LocaleUtil.isRTL()) rootView.setRotationY(180);
在我的例子中,我有另一个 fragment 作为 view pager 的子片段,所以在膨胀布局后我改变了 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;
}
}
更新
您可以使用 ViewPager2,它支持从右到左 (RTL),这将根据应用区域设置自动启用。
经过长时间的研究,我找到了一个非常简单的解决方案。没有反向适配器,旋转 child 在他们的 类 等
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)
}
}
重写为Java:
public class RtlViewPager extends ViewPager {
public RtlViewPager(@NonNull Context context) {
super(context);
}
public RtlViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
setRotationY(180f);
}
}
@Override
public void onViewAdded(View child) {
if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
child.setRotationY(180);
}
super.onViewAdded(child);
}
}
不需要任何第三方库。 Android 最近添加了名为 ViewPager2 的新 UI 组件,它支持 RTL 布局。
https://developer.android.com/jetpack/androidx/releases/viewpager2
请在下面link 中找到答案。答案谈到了迁移指南和示例 github 应用程序以支持 RTL 视图寻呼机。
对我有用:
ViewCompat.setLayoutDirection(binding.pager, ViewCompat.LAYOUT_DIRECTION_RTL);