片段转换不起作用

时间:2015-05-27 05:46:38

标签: android android-fragments android-animation

在我的应用程序中,我有3个标签(按钮),并且在它的滑动上我正在更改片段。所以我使用向左滑动并向右滑动动画。但它似乎对我不起作用。

Slide_Left

<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:propertyName="x"
        android:valueFrom="2000"
        android:valueTo="0"
        android:valueType="floatType" />
</set>

片段调用

try {
    String backStateName = fragment.getClass().getName();
    String fragmentTag = backStateName;


    FragmentManager manager = activity.getFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(id, fragment, fragmentTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.setCustomAnimations(R.animator.slide_in, R.animator.slide_out);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
} catch (Exception e) {

    e.printStackTrace();
}

2 个答案:

答案 0 :(得分:0)

使用 SlidingTabLayout ViewPager 代替此手动动画...... 请参阅以下链接...... 这很简单 https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

private class SlideAdapter extends FragmentStatePagerAdapter {
        public SlideAdapter(FragmentManager supportFragmentManager) {
        super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return your_first_fragment_object;
                case 1:
                    return your_first_fragment_object;
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Your First tab";
                case 1:
                    return "Your Second tab";
            }
            return super.getPageTitle(position);
        }

        @Override
        public int getCount() {
            return 2; // No of tab you want
        }

    }

多数民众赞成...... 现在只需使用setAdapter()将此适配器分配给ViewPager ...

答案 1 :(得分:0)

这可能会有所帮助

@Override
public Fragment getItem(int position) {

        switch (position) {
        case 0:

            frag = UsageRecoSummaryFragemrent.newInstance(position);
            // changeColor(newColor);

            break;
        case 1:
            frag = SmsDetailsFrag.newInstance(position);

            break;
        case 2:
            frag = CallDetailsFragment.newInstance(position);

            break;
        case 3:
            frag = MobileDataUsageFragment.newInstance(position);
            break;
        default:
            break;
        }
        fragmentReferences.put(position, new WeakReference<Fragment>(frag));
        return frag;
}