在我的TabLayout
项目中,我没有设置任何ViewPager
,并且使用tablayout positon提交了片段
tabsLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
attachChildMenuFragment(appPages.values()[tab.getPosition()]);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
在我的代码中,我想获取标签位置并使用条件来更改自定义动画,使其从左向右滑动或从右向左滑动,例如,如果当前的标签位置为3,我会用{{1 }}变量,如果制表符位置大于currentTabIndex
变量值,则动画必须从左向右滑动,否则动画必须从右向左滑动,例如:
currentTabIndex
不幸的是,所有动画都是从左到右,我不确定我的代码是否错误或动画xml不正确
if (mSelectedMenuItem.ordinal() > currentTabIndex) {
transaction.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
} else {
transaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}
@SuppressWarnings("ConstantConditions")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_instagram_profile, container, false);
...
currentTabIndex = 0;
appPages page = appPages.values()[0];
attachChildMenuFragment(page);
return mView;
}
@SuppressWarnings("ConstantConditions")
private void setupTabIcons() {
tabsLayout.addTab(tabsLayout.newTab().setIcon(tabIcons[0]));
...
tabsLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
attachChildMenuFragment(appPages.values()[tab.getPosition()]);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@SuppressWarnings("ConstantConditions")
private void attachChildMenuFragment(final appPages mSelectedMenuItem) {
if (selectedMenuItem == mSelectedMenuItem.ordinal()) {
return;
}
transaction = getActivity().getSupportFragmentManager().beginTransaction();
switch (mSelectedMenuItem.ordinal()) {
case 0:
fragment = new TimeLineFeeds();
break;
case 1:
fragment = new GridFeeds();
break;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mSelectedMenuItem.ordinal() > currentTabIndex) {
transaction.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
} else {
transaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}
transaction.replace(R.id.feeds_container, fragment);
transaction.commit();
}
}, 300);
selectedMenuItem = mSelectedMenuItem.ordinal();
currentTabIndex = mSelectedMenuItem.ordinal();
}
}
xml:
slide_in_from_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
xml:
slide_out_to_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
xml:
slide_in_from_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
xml:
slide_out_to_left