Android ViewPager:在幻灯片上销毁片段?

时间:2015-12-10 11:09:57

标签: java android android-layout android-fragments mobile

我基本上是在视图寻呼机的每个片段上播放一个动画。当用户滑动到特定片段时播放动画。但是,某些片段在我第二次访问动画时不会播放动画。这是因为视图寻呼机将它们保存在内存中。

我需要在用户滑动到另一个片段后销毁每个片段。这样,每当我重新访问这些片段时,动画就会播放。

主要观点:

pager = (ViewPager) findViewById(R.id.guidepager);
mAdapter = new NewUserGuideAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(0); //Tried this too. Didnt work

片段:

public class NewUserPage_Two extends Fragment {

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

        //Play animation, etc
       Animation animation_1 = AnimationUtils.loadAnimation(NewUserPage_Two.this.getActivity(), R.anim.abc_slide_in_bottom);
        person1.setAnimation(animation_1);

      return rootView;
}

适配器:

public class NewUserGuideAdapter extends FragmentPagerAdapter {

    public NewUserGuideAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new NewUserPage_One();
            case 1:
                return new NewUserPage_Two();
            case 2:
                return new NewUserPage_Three();
            case 3:
                return new NewUserPage_One();
            case 4:
                return new NewUserPage_One();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 5;
    }
}

如何修改我的代码?

3 个答案:

答案 0 :(得分:0)

在片段内尝试:

((BaseAdapter) *YourContainer*.getAdapter()).notifyDataSetChanged();


您可以参考:Refresh Current Fragment (ListView Data) remaining in the same activity

答案 1 :(得分:0)

ViewPager提供方法mViewPager.setOffscreenPageLimit(0);

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

答案 2 :(得分:0)

            <ul class="nav navbar-nav navbar-right">
                <li>
                    <p class="navbar-btn btn-space btn-top-space">
                        <a href="https://url.com" target="_blank" title="Customer Payment Panel (CPP)" class="btn-sm btn-info">
                            <span class="glyphicon glyphicon-user"></span> <strong>Customer Panel</strong> (CPP)
                        </a>
                    </p>
                </li>
                <li>
                    <p class="navbar-btn btn-top-space">
                        <a href="https://url.com" target="_blank" title="Control Panel (Plesk)" class="btn-sm btn-success">
                            <span class="glyphicon glyphicon-cog"></span> <strong>Control Panel</strong> (Plesk)
                        </a>
                    </p>
                </li>
            </ul>

顺便提一下方法体public void setOffscreenPageLimit(int limit) { if (limit < DEFAULT_OFFSCREEN_PAGES) { Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " + DEFAULT_OFFSCREEN_PAGES); limit = DEFAULT_OFFSCREEN_PAGES; } if (limit != mOffscreenPageLimit) { mOffscreenPageLimit = limit; populate(); } } 。我认为谷歌添加此限制因为您在滑动时至少需要当前项目之间的2个视图。 您可以尝试使用DEFAULT_OFFSCREEN_PAGES=1并在addOnPageChangeListener()上开始动画。