Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update the current fragment in FragmentPagerAdapter

I have a viewPager with tab indicator. The ViewPager is setAdaper with a FragmentPagerAdapter.

I have little understanding how the internals of FragmentPagerAdapter work. I noticed that the neighbor fragments are resumed ( OnResume is called ) , even though the neighbor are not yet visible.

I put the update methods in OnResume thinking that once the fragment is current, it will be updated.

Ad banner refresh
I want the ad banner set in the footer to update when swiping to the left once or swipping once to the right . The neighbor fragments are not recreated ( good thing ). But onResume is already called avoiding the banner refresh. The method loadBannerAd is in OnResume() method.



How can I call the method loadBannerAd() only for the current fragment by a method inside the fragment ?

EDIT : I already know about mViewPager.setOnPageChangeListener() .

    OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
        // this method can be called before the fragment 's onCreateView()
        }

        ....
    };

But there is a danger when the fragment has not been created yet . Managing whether the fragment has been created or not in the Activty defeats the purepose of it.

enter image description here

like image 767
Raymond Chenon Avatar asked Apr 18 '13 11:04

Raymond Chenon


People also ask

How do I refresh FragmentPagerAdapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

How do I refresh a ViewPager fragment?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

How do I upgrade one fragment to another?

User Edit in editfield and press button(from FragmentA) then a textview should be updated that is on fragmentB on same Screen. YourNewFragment ldf = new YourNewFragment(); Bundle args = new Bundle(); args. putString("YourKey", "YourValue"); ldf. setArguments(args); // Inflate the fragment getFragmentManager().

How do you refresh a Java fragment?

Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so.


1 Answers

You simply use:

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

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

            }

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

            }

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

            }
        })

Then you can handle what happens in onPageSelected(int index).

like image 190
Warpzit Avatar answered Sep 28 '22 08:09

Warpzit