简体   繁体   English

Android从片段中检测ViewPager上的OnPageChangeListener

[英]Android detecting OnPageChangeListener on a ViewPager from within a fragment

Hi i have a ViewPager that has creates a fragment for each page using a fragment adapter. 嗨,我有一个ViewPager,它已使用片段适配器为每个页面创建一个片段。 On each fragment i have an ImageView that i want to animate everytime that page appears on screen. 在每个片段上,我都有一个ImageView,每次该页面出现在屏幕上时都希望对其进行动画处理。

My ViewPager currently preloads the 1st 2 pages so the animation doesn't run on those but it does work on the 3rd page. 我的ViewPager当前会预加载第1页和第2页,因此动画不会在第2页上运行,但在第3页上有效。 I have added an onPageChangedListener but not sure how to implement it so it talks to my fragment adapter and runs the animation. 我添加了一个onPageChangedListener,但不确定如何实现它,因此它与我的片段适配器通信并运行动画。 Does anyone know either how to correctly detect a page change from within a fragment or a way to disable the page preloading in a memory management efficient way? 有谁知道如何从片段中正确检测页面更改,或者以有效的内存管理方式禁用页面预加载?

heres what i have tried so far 到目前为止我一直在尝试

heres my activity with the ViewPager 我在ViewPager中的活动

public void getAdapter(){
         CustomViewPager pager = (CustomViewPager) findViewById(R.id.tutorialPager);
         pager.setPagingEnabled(true);
         pager.setOffscreenPageLimit(-1);
         adapterViewPager = new MyPagerAdapter(getSupportFragmentManager(),this);
         pager.setAdapter(adapterViewPager);
         CirclePageIndicator Indicator = (CirclePageIndicator)findViewById(R.id.circles);
         Indicator.setViewPager(pager);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_tutorial_view);
        getAdapter();
        extras = getIntent().getExtras();
    }


        public static class MyPagerAdapter extends FragmentPagerAdapter {

            private Context context;

            public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager,Context c) {

                super(fragmentManager);
                context = c;
            }


            @Override
            public int getCount() {
                int levelCount = 4;
                return levelCount;
            }

            // Returns the fragment to display for that page
            @Override
            public TutorialAdapter getItem(int position) {
                String data = null;

                return TutorialAdapter.newInstance(position, data, context);

            }


            @Override
            public CharSequence getPageTitle(int position) {
                return "Page " + position;
            }

        }

       public static class onPageChangedListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub

        }

       }

    }

heres my fragment adapter inside my onCreateView 这是我的onCreateView中的片段适配器

if(page == 1){
                view = inflater.inflate(R.layout.layout_tutorial, container, false);


                tutImage = (ImageView)view.findViewById(R.id.tutImage);
                int imageResource = getResources().getIdentifier("tut_view_2", "drawable", context.getPackageName());
                Drawable tutImageDrawable = getResources().getDrawable(imageResource);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tutImage.setBackground(tutImageDrawable);
                }else{
                    tutImage.setBackgroundDrawable(tutImageDrawable);
                }

                Animation AniMoveUp = AnimationUtils.loadAnimation(context, R.anim.tut_image);  
                tutImage.startAnimation(AniMoveUp);

Here is an example Android application that extends the Fragment Life Cycle with two new methods: onResumeViewPage() and onPauseViewPage(). 这是一个示例Android应用程序,它使用两种新方法扩展了片段生命周期:onResumeViewPage()和onPauseViewPage()。 If you put your animation code in onResumeViewPage() it should work. 如果将动画代码放在onResumeViewPage()中,它将可以正常工作。

See: Slider Android Application in BitBucket. 请参阅:BitBucket中的Slider Android应用程序

Move your startAnimation call to the onResume method of the Fragments. 将您的startAnimation调用移至Fragments的onResume方法。 That way the animation will start when the Fragment is visible. 这样,当“片段”可见时,动画将开始。 See Fragment LifeCycle for more detailed explanation. 有关更多详细说明,请参见Fragment LifeCycle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM