I’m using a ViewPager in my app, with the initial fragment being a ListView and the other fragments being details of the ListView. The user can either manually select an item in the ListView or swipe though the various details. I have the swipe working, but I’m not sure how to get the item selection working. I’m assuming I need to do something in onListItemClick of the ListView and I know I can use setCurrentItem of the ViewPager, but the ViewPager is in the Activity and the ListView is in the fragment so I’m not sure how to get both working.
This might be something obvious, but I’m new to using a ViewPager and there’s doesn’t seem to be alot of documentation out there, mostly tutorials on getting a basic ViewPager working.
You have three ways of setting up fragment/activity communication:
You can define an interface aka a Listener that the activity implements.
Reference: http://developer.android.com/guide/components/fragments.html Check out the section called “Creating event callbacks to the activity”
Use an EventBus library such as Otto or EventBus to create a publisher/subscriber system where your activity is the subscriber and your fragments publish events.
You can use the LocalBroadcastManager to send out a message through intents that the activity intercepts by creating a BroadcastReceiver with an action string specified by you. Heres an example of a typical use case: how to use LocalBroadcastManager?