Fix tabLayout swipe direction
- Fix swipe direction by add layoutDirection=ltr in xml - Reverse viewPager tab index when layoutDirection is rtl Fixes: 147415514 Test: 1. change to rtl language 2. Settings -> Account 3. Swipe from left to right, should move tab to left. 4. Swipe from right to left, should move tab to right Change-Id: Ib5a080f4706161fd9c0f4d7f2da4d7b98946d960
This commit is contained in:
@@ -66,6 +66,7 @@
|
|||||||
android:id="@+id/tabs"
|
android:id="@+id/tabs"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layoutDirection="ltr"
|
||||||
app:tabMaxWidth="0dp"
|
app:tabMaxWidth="0dp"
|
||||||
app:tabGravity="fill"
|
app:tabGravity="fill"
|
||||||
app:tabMode="fixed"
|
app:tabMode="fixed"
|
||||||
|
@@ -24,6 +24,7 @@ import android.content.Context;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@@ -45,6 +46,7 @@ import com.google.android.material.tabs.TabLayout;
|
|||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base fragment class for profile settings.
|
* Base fragment class for profile settings.
|
||||||
@@ -93,6 +95,9 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
|
|||||||
* Used in fragment argument with Extra key {@link SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB}
|
* Used in fragment argument with Extra key {@link SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB}
|
||||||
*/
|
*/
|
||||||
public static final int WORK_TAB = 1;
|
public static final int WORK_TAB = 1;
|
||||||
|
private static final int[] LABEL = {
|
||||||
|
R.string.category_personal, R.string.category_work
|
||||||
|
};
|
||||||
|
|
||||||
private ViewGroup mContentView;
|
private ViewGroup mContentView;
|
||||||
|
|
||||||
@@ -101,7 +106,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
|
|||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
mContentView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
|
mContentView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
final int selectedTab = getTabId(activity, getArguments());
|
final int selectedTab = convertPosition(getTabId(activity, getArguments()));
|
||||||
|
|
||||||
final View tabContainer = mContentView.findViewById(R.id.tab_container);
|
final View tabContainer = mContentView.findViewById(R.id.tab_container);
|
||||||
final ViewPager viewPager = tabContainer.findViewById(R.id.view_pager);
|
final ViewPager viewPager = tabContainer.findViewById(R.id.view_pager);
|
||||||
@@ -180,7 +185,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(int position) {
|
public Fragment getItem(int position) {
|
||||||
return mChildFragments[position];
|
return mChildFragments[convertPosition(position)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -190,11 +195,15 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getPageTitle(int position) {
|
public CharSequence getPageTitle(int position) {
|
||||||
if (position == 0) {
|
return mContext.getString(LABEL[convertPosition(position)]);
|
||||||
return mContext.getString(R.string.category_personal);
|
|
||||||
} else {
|
|
||||||
return mContext.getString(R.string.category_work);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int convertPosition(int index) {
|
||||||
|
if (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())
|
||||||
|
== View.LAYOUT_DIRECTION_RTL) {
|
||||||
|
return LABEL.length - 1 - index;
|
||||||
|
}
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user