ViewPager update fragment on swipe

前端 未结 4 729
孤城傲影
孤城傲影 2020-12-01 00:14

i have a problem that i have been struggling with for the past 2 days.

I am building an app that uses ActionBar, ViewPager & FragmentPagerAdapter. The code for

相关标签:
4条回答
  • 2020-12-01 00:41

    Surprisingly the ViewPager doesn't do this "natively" (among other things). But not all is lost.

    First you have to modify your fragments so they only run the animation when you tell them that it's ok and not when they are instantiated. This way you can play with the viewpager offset (default = 3) and have 2-3 fragments preloaded but not animated.

    Second step is to create an interface or similar that defines when the "fragment has become visible".

    Third step would be to attach a new OnPageScrollListener to your viewpager.

    Code follows (in semi-untested-code):

    1) Attach the Listener:

    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(final int i, final float v, final int i2) {
                }
                @Override
                public void onPageSelected(final int i) {
                    YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, i);
                    if (fragment != null) {
                        fragment.fragmentBecameVisible();
                    } 
                }
                @Override
                public void onPageScrollStateChanged(final int i) {
                }
            });
    

    2) This is your Interface:

    public interface YourFragmentInterface {
        void fragmentBecameVisible();
    }
    

    3) Change your fragments so they implement this:

    public class YourLovelyFragment extends Fragment implements YourFragmentInterface {
    

    4) Implement the interface in the fragment

    @Override
    public void fragmentBecameVisible() {
        // You can do your animation here because we are visible! (make sure onViewCreated has been called too and the Layout has been laid. Source for another question but you get the idea.
    }
    

    Where to go from here?

    You might want to implement a method/listener to notify the "other" fragments that they are no longer visible (i.e. when one is visible, the others are not). But that may not be needed.

    0 讨论(0)
  • 2020-12-01 00:41

    This will affect the changes from one page to another page in view pager.

    OnPageChangeListener  pagechangelistener =new OnPageChangeListener() {
    
                    @Override
                    public void onPageSelected(int arg0) {
                        Logger.logMessage("Called first");
    
                        pageAdapter.notifyDataSetChanged();
                        indicator.setCurrentItem(arg0);
                    }
    
                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
    
                        Logger.logMessage("Called second");
    
                    }
    
                    @Override
                    public void onPageScrollStateChanged(int arg0) {
    
                        Logger.logMessage("Called third");
    
                    }
                };
                myViewPager.setOnPageChangeListener(pagechangelistener);
    

    Use this in your page adapter.

    @Override
        public int getItemPosition(Object object) {
    
            return POSITION_NONE;
        }
    
    0 讨论(0)
  • 2020-12-01 00:48

    Add this code when you define the Viewpager

    mViewPager.setOffscreenPageLimit(0);
    

    It will not save any view of the fragments when you switch to other fragment .. so switching from fragment to another will create the fragment from the start and update everything.

    0 讨论(0)
  • 2020-12-01 00:58

    setOnPageChangeListener has been deprecated. Instead, you should use addOnPageChangeListener.

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int i, final float v, final int i2) {
            }
            @Override
            public void onPageSelected(final int i) {
                YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, i);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                } 
            }
            @Override
            public void onPageScrollStateChanged(final int i) {
            }
        });
    
    0 讨论(0)
提交回复
热议问题