3

I'm using a ViewPager ( http://developer.android.com/reference/android/support/v4/view/ViewPager.html ) with some images in Fragments, and because I want to support zooming I am extending ViewPager and overriding:

onInterceptTouchEvent

The next step is to determine whether or not the image is zoomed in. If it is zoomed in I'll let it handle the event, otherwise I'll pass the event to super.

My main problem, and the heart of my question, is determining the best and most efficient way to communicate between the current fragment and my ViewPager?

Currently I'm using the following when the current item changes:

Fragment photoFrag = (Fragment) getAdapter().instantiateItem(this, getCurrentItem()); 

But it feels as though there should probably be a better way to communicate between the Fragment and the ViewPage and I'm missing something.

Edit: Sorry I did forget to mention that I'd like the communication to go back and forth, so I'd like the fragment to be able to communicate with the ViewPager, and the Pager with the fragment. I saw an example (URL left at work) where an Interface class was used to communicate between the current Fragment and the ViewPager. So the underlying image needs to tell the ViewPager when it should handle the Scroll, when it doesn't need to, and when to fake scroll, etc.

I was rushing to get this question up during the Android Hangout

Thanks

selsine
  • 2,833
  • 2
  • 19
  • 22
  • 1
    For a slightly better nasty hack, see http://stackoverflow.com/a/9293207/115145 – CommonsWare Mar 21 '12 at 22:42
  • 1
    @CommonsWare Thanks for the tip, both methods seem pretty similar since instantiateItem pretty much does the same thing internally. Strange that there isn't a cleaner way to do this. – selsine Mar 21 '12 at 23:39
  • 1
    "Strange that there isn't a cleaner way to do this." -- agreed. – CommonsWare Mar 22 '12 at 00:07
  • This is what I'm basing a lot of my code off of: http://multitouchdesign.wordpress.com/2011/11/19/extending-android-viewpager/ – selsine Mar 23 '12 at 19:23
  • Do not use that with a `FragmentStatePagerAdapter`. The point behind `FragmentStatePagerAdapter` is to allow fragments that are not visible to be garbage collected. The `HashMap` in the solution you link to will inhibit this. You might be able to pull it off by using `SoftReference` values for the map. – CommonsWare Mar 23 '12 at 19:37
  • @CommonsWare Sorry I missed your comment. I'm not using the hashmap method that the author on that blog post is using. I've seen it elsewhere but was overkill for what I was doing. Plus I need my fragments to be garbage collected. – selsine Mar 28 '12 at 21:27
  • "Strange that there isn't a cleaner way to do this." -- agreed 2. P.S. stupid android.support.v4 v7 v1000, stupid viewpager, stupid fragment ... and so on. – thecr0w Oct 29 '12 at 12:35
  • "Strange that there isn't a cleaner way to do this." -- agreed x3 – forcewill Oct 14 '14 at 10:51

1 Answers1

2

Why not use a BroadcastReceiver to send/receive an event? Register the fragment(s) to receive the event and then you won't have to worry that your fragment does not exists. Don't forget to register/unregister receiver in onResume/onPause

Bostone
  • 34,822
  • 38
  • 158
  • 216