0

I have an app with two fragments and I want to implement the ViewPager and ActionBarTabs to it. My question is: Is there any way to recreate my ListFragment each time when user moves camera in my MapFragment ? Each move assigns different values to an array which I use to populate the ListFragment. I have looked at this link Synchronize all the ListView in a ViewPager but it looks too complicated. Please help. Take in consideration that I can't use notifyDataSetChanged() on adapter because I want to keep my MapFragment as it is and not recreate it.

Community
  • 1
  • 1
Lukas Anda
  • 716
  • 1
  • 9
  • 30

1 Answers1

0

You could try making an Interface with an update method like so

public interface UpdatingFragment {
public void update();}

Then have your ListFragment implement this interface. And in the update method recreate the list in your ListFragment.

public class MyListFragment extends ListFragment implements UpdatingFragment{

@override
public void update(){
//whatever code you use to update your fragment
}

Then call this method from your PagerAdapter

@Override
public int getItemPosition(Object item) {
    if (item instanceof UpdatingFragment) {
        ((UpdatingFragment) item).update();
    }
    //don't recreate fragment 
    return super.getItemPosition(item);
}

Now if you call notifyDataSetChanged() on your viewpager adapter, it should hopefully update your ListFragment without affecting the map. Call this every time the user moves the camera in the map fragment.

funkomatic
  • 76
  • 6
  • Can you post some bigger snippet about what to put where ? I mean the recreation of a ListFragment, where to put the notifyDataSetChanged and so – Lukas Anda Jun 05 '15 at 20:28
  • I was interested in that part about updating the list frsgment, but thabks for your response. – Lukas Anda Jun 08 '15 at 16:21
  • 1
    Well I'm not really sure how specifically you would implement this for your problem. You said you update an array each time the map is moved and then you populate your list with this array. So if the array is already set to the list then all you'd have to do is call notifyDataSetChanged() from inside the update method and it should work. So when you move the map you would update your array as you say you're already doing. Then call notifyDataSetChanged() on your ViewPager adapter. This will update your ListFragment due to the code I wrote above and finally the update method will be called – funkomatic Jun 08 '15 at 16:48
  • Yes, this is what I am looking for. Thanks :) – Lukas Anda Jun 09 '15 at 04:40