使用ViewPager setCustomAnimations的片段无法正常工作

时间:2016-07-24 18:54:29

标签: android android-fragments android-viewpager

我让FragmentTransactions对所有其他片段工作得非常好但是当移动到仅包含ViewPager的片段时,不会显示动画并且突然出现新片段。点击后退按钮时,它正确地离开,前一个片段进入。是因为我使用ViewPager会发生这种情况吗?

MainActivity:

fm.beginTransaction()
     .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
     .hide(itineraryFragmentReturn)
     .add(R.id.fragment_container, emailFragment, "email_fragment")
     .addToBackStack(null)
     .commit();

emailFragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical">

    <CustomViews.VerticalViewPager
        android:id="@+id/verticalViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </CustomViews.VerticalViewPager>

</FrameLayout>

EmailFragment.class:

public class EmailFragment extends Fragment
{
    private static final int NUM_PAGES = 2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private List<Fragment> fragments;
    private View thisView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.email_fragment, container, false);

        thisView = view;

        return view;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        loadAdapter();
    }

    private void loadAdapter()
    {
        fragments = new ArrayList<>();

        mPager = (ViewPager) thisView.findViewById(R.id.verticalViewPager);

        fragments.add(new LocationsFragment());
        fragments.add(new AskEmailFragment());

        //getChildFragmentManager() very important for ViewPager
        mPagerAdapter = new ScreenSlidePageAdapter(getChildFragmentManager(), fragments);
        mPager.setAdapter(mPagerAdapter);
        mPager.setOffscreenPageLimit(1);
        mPager.setCurrentItem(1);
    }


    private class ScreenSlidePageAdapter extends FragmentPagerAdapter
    {
        private List<Fragment> fragments;

        public ScreenSlidePageAdapter(FragmentManager fragmentManager, List<Fragment> fragments)
        {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public int getCount()
        {
            return NUM_PAGES;
        }

        @Override
        public Fragment getItem(int position)
        {
            return this.fragments.get(position);
        }
    }
}

我已经尝试过评论mPager.setCurrentItem(1);行,以防导致动画不足但不是。

理想情况下,我想要的是使用包含开始的进度条的自定义动画显示Fragment,然后在后台加载ViewPager然后显示它。我可以轻松地显示ProgressBar,但我如何以编程方式附加ViewPager,我会假设它在onCreate中初始化,然后显示视图?

0 个答案:

没有答案