Setting ViewPager's current item from fragment - android

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?

Related

Is there a more efficient way to implement event listeners in nested fragments?

Right now, my application has a single activity and 4 fragments:
ViewPager fragment.
2X fragments that contain a RecyclerView each.
A details fragment populated on the basis of the item chosen.
I read that it's in good practice to implement listeners to handle events in fragments and let the activity handle events and communication between the fragments. But to make it possible, I've used too many levels of listeners and my code seems to complicated. Is there a better way ? One example of the sequence of listeners is:
Listener in RecyclerView Adapter --> Listener in RecyclerView Fragment -->
Listener in ViewPager --> Listener in MainActivity --> Listener in info Fragment.
And my ViewPager's in a fragment so I could create a flexible UI to support tablets as well.
Look at Eventbus: https://github.com/greenrobot/EventBus
Have all your fragments and activity register themselves on the event bus. Whenever you want to trigger an event, simply post an event and any fragment/activity that is accepts that event will receive it and react accordingly.
Just name your events appropriately and your code should be easier to understand.

Where should use PageViewer?

I have implemented ViewPager and number of Fragment as child, here every child override own onAttach, onCreateView, onViewCreated and setUserVisibleHint.
In my app navigation behaviour is random, it not be in sequence every time. Since page viewer perform caching to load extra child, and this is what my problem is. I am not sure exactly when I should initialise/release member of child class.
Required suggestion from you guys, will it be preferable to use PageViwer in this case or I should go with traditional activity flow for each of component.
ViewPager is typically used for move efficient horizontal item to item navigation. Typical use cases would be
Swiping through the related items (e.g. emails, images, songs of an album, etc.)
Swiping between the tabs
Swiping back-and-forth in a wizard-like activity
For more details you can read a section about Swipe Views Android Design pattern.
Regarding the lifecycle, it basically uses the same lifecycle as any other Fragment. The only difference is, that lifecycle methods can be called a bit later or earlier than you expect, because of fragment's caching ViewPager implements.
I am not sure exactly when I should initialise/release member of child class.
You should basically rely on two methods: onStart() and onStop(). In onStart() you create class members and initialize everything you want to. In onStop() method you should deinitialize everything and remove all listeners you set in onStart().
Method setUserVisibleHint() is used independently of onStart() or onStop(). You shoud better not initialize or destroy anything in there. You must not consider it to the a lifecycle method, because it's not. It's there just to give you a hint, that your fragment is visible to the user. Here you can start or stop animation, or request data update or perform similar tasks. This is the only purpose of this method.
Required suggestion from you guys, will it be preferable to use
PageViwer in this case or I should go with traditional activity flow
for each of component.
If your activity fits into one of the points mentioned about, I would suggest you to use ViewPager. Otherwise you might consider other options.
Update: Most likely you won't override onCreate() and onDestroy() lifecycle methods of a fragment very often. You will use onCreateView() and onDestroyView() methods instead. There you can implement so called static initialization, the initialization which doesn't change while a fragment is still alive. This is layout initialization and similar tasks.
ViewPager Usages
Screen slides are transitions between one entire screen to another and are common with UIs like setup wizards or slideshows.
If you have good knowledge in Fragment than ViewPager is right component for implements.
Because viewpager provide a place which you can add fragment runtime.
For eg.
If you want to use TabBar in your project than viewpager is right component for using. Because it's provide a place which you can add Fragment runtime. Tabbar is common in android application. It's provide lot of functionality inbuild we can use to add fragment runtime.
Facebook app using ViewPager to manage tab. Viewpager provide smoothness of your application.
You can Download example from this url and check your requirement fulfill or not
You can download the Example here
ViewPager
It is an widget
Allows the user to swipe left or right to see an entirely new screen.
Easy to show the user multiple tabs
Dynamically add and remove pages (or tabs) at anytime.
To Read More: http://architects.dzone.com/articles/android-tutorial-using

Android Navigation Drawer nested activity vs fragment approach

first, I'd like to state that I'm new to android development and to gradle as well. not to java.
I had a project supporting 2.3.6 and I'm now in the process of migrating it to 4.4 using the support library.
I'm using Gradle as well, and so far, it's working great.
I'm now using the navigation drawer to navigate between the app's fragments, which used to be activities.
One of the fragments in the drawer holds a listview and a detail view for an item in the list.
The drawer fragment switches it's view when the user clicks an item in the list and shows it's details.
While it's pretty clear to me why it's not recommended for 2 fragments to communicate directly, I still need the list fragment to tell it's details fragment counterpart about the selected item.
The guides specifically say that the activity should implement the callbacks and on the callback methods it should communicate the data between the fragments.
However, in my case, the parent is also a fragment.
My question is should the parent fragment maintain the callbacks?
It seems like the proper solution, since my fragment is only implemented this way to use the navigation drawer swap, and logically, it behaves like an activity, but this would go against the guides saying fragments should not communicate directly.
The question is if implementing the callbacks in the containing fragment is a viable approach or i should change this part of the app to an activity?
Thanks, and sorry for the wall of text.
Your parent fragment should be swapped in via the NavigationDrawerFragment whose callbacks should be implemented in the Activity. The callbacks shouldn't have anything to do with the data passing if I'm understanding you correctly. If the detail is a child of the parent fragment I don't see any reason that the data can't be passed when the child is created.

Handling fragment transaction in android.

Hi I am developing android application in which I am using one activity and two fragments. Consider same example which google explain like one list view and detail view. on click of list item we are rendering respective detail fragment.
So I learn how to do fragment transaction and i come up with two solutions. One which is standard way which google explain that make one interface and implement that interface into main activity. And do fragment transaction there inside main activity.
I tried with another way. when I click on list item inside click listener instead of calling interface I change fragment inside my list fragment only and its working fine.
So i want to know what is difference between those to methods. changing fragment from main activity and changing it from fragment only.
What kind of problem i will face if i implement with second method.i.e. changing from fragment only.
Need Help. Thank you.
What kind of problem i will face if i implement with second
method.i.e. changing from fragment only.
There isn't an actual problem, it's more of a design discussion. Using the second approach means you're making a very specific fragment, one that on a click on one of its rows will make a transaction with a specific fragment on a specific place of the holder activity. This would be a problem if you plan on reusing this fragment.
Suppose you have this ListFragment and you decided that it should be used in five other activities(with different data). Because it has a very precise action when clicking one of its rows, this fragment will always require the holder activity to have a specific container(where the transaction will be done) along the specific detail fragment with which it was initially used. The problem is that in each of those five activities you may want to use a different fragment when clicking a row of the ListFragment, this would require making changes to the class of the ListFragment.
Now suppose you have the same behavior with the interface approach. As the ListFragment doesn't know or care who handles that click event(as it passes it to whoever registers as the listener) you can simply put the ListFragment in the five activities with no problem(so no changes to it at all). In the interface method of the activity you could then implement the desired behavior with whatever fragment you want and in whatever container setup you want.

Filtering ListView which is part of a ViewPager Fragment

I recently converted my application from using Activites and TabHost to using Fragments and ViewPager from the Android Compat Library for API v4
I was able to fix/resolve most problems but am unable to retain the previous behavior with filtering text in ListViews using the setTextFilterEnabled method.
My ViewPagerAdapter contains Fragments which each have a ListView. As users swipe through the ViewPager, I would like the currently active Fragment's ListView to filter text as users type, just like I was able to do with the TabHost-Activity model. Currently, it looks like the first Fragment's ListView will respond correctly, but if I swipe to the next Fragment and try to filter its ListView, the filtering will still apply to the first one. If I swipe past the first two and then filter, the results are non deterministic. The currently active Fragment will never apply the filter. Sometimes a neighboring fragment will, sometimes it won't.
I tried to fix this by adding custom callbacks which let me monitor which Fragment is currently visible, and which are hidden. When a fragment becomes visible as the main Fragment of the ViewPager, I set the setTextFilterEnabled on its ListView to true, and set all others to false. This didn't seem to help at all (I verified that I was toggling the flag correctly for the right Fragments).
I suspect this needs some kind of deeper integration with ViewPager, but I can't really figure out what I need to wire up. Any ideas on how I can make this work? I'm happy to muck with the ACL code if needed.
You could implement the method onPageSelected extending OnPageChangeListener. Perhaps you already do this. From there you can set the adapter to the current ListView or requery your cursorAdapter if that's the case. Note that the Adapter used must implement the Filterable interface.

Categories

Resources