I'm trying to add a ViewPager
(using FragmentStatePagerAdapter) to my current app. What I've got so far, is a master/detail setup, where the master is a GridView
and the detail is (essentially) an ImageView
. I'm using a CursorLoader
to load a cursor, and "connect" it to the grid view using a CursorAdapter
.
Click an image in the grid, and MainActivity
starts a DetailActivity
with the corresponding IMAGE_ID in an URI. The DetailFragment
, in turn, has its own CursorLoader
to get that specific image and set it in the UI.
Nice stuff, except the user has to go back-and-forth to switch images. I obviously want to be able to swipe left and right (on an image/detailview) to jump to previous/next image! This -- I've learned -- is where a ViewPager
comes into play.
I found a nice tutorial, but I'm a bit confused as to how this all "meshes" with my current code. As I said, I have a CursorAdapter which populates the GridView. I take it I'll be keeping that, unchanged? However, my DetailFragment is where I understand I'll be needing some new code. Some thoughts and questions, then:
The tutorial's
ItemPagerFragment
uses a custom adapter,CursorPagerAdapter extends FragmentStatePagerAdapter
. This is to "pre-load" the detail fragments, and handle the swipes and lateral navigation. I need to add this to my project (but this does not replace my CursorAdapter)?The
ItemPagerFragment
in the tutorial has a CursorLoader, and I'm not sure what that actually does. However, at a glance, I would assume the task of this loader is to get all images, such that the fragment can (somehow) "prepare" all the "pages" (ItemDetailFragments -- see below) the user can navigate between?The
ItemPagerFragment
in the tutorial "holds" the FragmentStatePagerAdapter, and inflates theViewPager
UI. I'm thinking I'll need to add such a class to my app, with a CursorLoader to load all images in theMediaStore
.Each "page" of detail view (images) is then an instance of
ItemDetailFragment
. As I understand, this is equivalent to my current DetailFragment. It takes an ID or an index/position (or something) and loads the corresponding image into the ImageView (using a ContentLoader from theMediaStore
).But how do I handle the user clicking a specific image in the gridview, to load it? The CursorPagerAdapter's
getItem()
method returns anItemDetailFragment
, so maybe I could use this to get the desired fragment? But how to pass in (request) the specific image_id?