Viewpager Update Fragment on Swipe

ViewPager update fragment on swipe

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.

Android ViewPager refresh when swipe

If you want to refresh the data in a particular Fragment, you have to do 2 things,

  1. Store the updated data into Activity
  2. Show the updated data in onResume() in that particular Fragment

If you want your data to be saved even after killing the app, store it in SharedPreference. Hope it helps!

Android ViewPager Refresh fragment

I am sure this is not the only/best way to do it. But this should solve the problem as it did to me.

Please write refresh() methods in both
FragmentCanali and FragmentPreferiti Fragments.

    public class ListCanali extends Fragment implements OnPageChangeListener{
private MyAdapter mAdapter;

@Override
public View OnCreateView (...) {
...
if(mAdapter == null) {
mAdapter = new MyAdapter(getChildFragmentManager());
}
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(this);
...
}

...
@Override
public void OnPageSelected (int position) {
switch (position){
case 0 : ((FragmentCanali)adapter.getItem(position)).refresh();
case 1 : ((FragmentPreferiti)adapter.getItem(position)).refresh();
}
}}

It will be cleaner if you extend both the fragments from a base fragment which has abstract refresh() method.

In the onPageSelected() you can just call ((BaseFragment)adapter.getItem(position)).refresh()

How to refresh or update all fragment from the viewpager android

You can use EventBus, put this in all your fragments:

 @Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}

@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(ViewEvent viewEvent) {
if (viewEvent.getMessage().equals("updateViews")) {
updateView();

}

}

and when you want to update you can send an event like this:

 EventBus.getDefault().post(new ViewEvent("updateViews"));

PS: ViewEvent is an object that you must create with what to send to all suscribed fragments.

public class ViewEvent {

private String message;

public ViewEvent( String message) {

this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

Update Fragment from ViewPager

Update Fragment from ViewPager

You need to implement getItemPosition(Object obj) method.

This method is called when you call

notifyDataSetChanged()

on your ViewPagerAdaper. Implicitly this method returns POSITION_UNCHANGED value that means something like this:
"Fragment is where it should be so don't change anything."

So if you need to update Fragment you can do it with:

  • Always return POSITION_NONE from getItemPosition() method. It which
    means: "Fragment must be always recreated"
  • You can create some update() method that will update your
    Fragment(fragment will handle updates itself)

Example of second approach:

public interface Updateable {
public void update();
}

public class MyFragment extends Fragment implements Updateable {

...

public void update() {
// do your stuff
}
}

And in FragmentPagerAdapter you'll do something like this:

@Override
public int getItemPosition(Object object) {
MyFragment f = (MyFragment ) object;
if (f != null) {
f.update();
}
return super.getItemPosition(object);
}

And if you'll choose first approach it can looks like:

@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

Note: It's worth to think a about which approach you'll pick up.



Related Topics



Leave a reply



Submit