简体   繁体   中英

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. On each fragment i have an ImageView that i want to animate everytime that page appears on screen.

My ViewPager currently preloads the 1st 2 pages so the animation doesn't run on those but it does work on the 3rd page. I have added an onPageChangedListener but not sure how to implement it so it talks to my fragment adapter and runs the animation. 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

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

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(). If you put your animation code in onResumeViewPage() it should work.

See: Slider Android Application in BitBucket.

Move your startAnimation call to the onResume method of the Fragments. That way the animation will start when the Fragment is visible. See Fragment LifeCycle for more detailed explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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