Android-fragment,viewpager,activity relation - android

I have one activity(MainActivity) and corresponding xml file. Xml file contains the toolbar on the top, then the sliding tabs then then viewpager.
I have 3 fragments(corresponding xml files) and 3 SlidingTabs. Tab 1 is connected with fragment 1 and 2 with 2 and 3 with 3.
If I am performing any operations on the views in the fragments, Do I need to write all the code in MainActivity or in the Fragment?
Do I have to return only the view from the fragment for that pager or all the codes for that page should also be there inside fragment?
For Ex:
If I am retrieving some data using contentresolver and populating the data on the listview in 1st fragment, Likewise, If I am doing some other operations and displaying the details in fragment 2 and 3, Do I need to write all the codes in the corresponding fragment itself or I have to write in MainActivity.
Can someone provide the answer to this or any link which will be useful.

When you are using Tabs with View pager, then MainActivity will just act as container and it holds Fragments.
So you can write respective code on respective fragments only. Any thing needed as common for all fragments then you can write in MainActivit. For Example Tool bar title changes, Option menu's ect..

Yes you have to return only the view from the fragment for that pager.And all the codes for that page should be inside fragment itself.
Only common codes or if you want to define any call back methods from the fragment, their implementations can we done in your activity.
Refer the below link where you can get an idea of your tabs implementation with few common material design controls.
https://github.com/chrisbanes/cheesesquare

Related

Android TabLayout & Fragments

I have an Activity which contains a TabLayout and a ViewPager. The first tab contains a calendar and the second tab contains a timer.
Tapping on a date in the first tab should display a fragment with some event details while simultaneously hiding or removing that menu tab.
To this end, how can I use a FrameLayout as a fragment container when my layout already contains a ViewPager? Should I create a new Activity to get what I want, or is there a more efficient solution?
What you are describing is basically a list/details pattern. In short, yes, you should create a new activity for the details. It is the expected flow of an android app and it makes the implementation for you so much easier.

use of multiple fragments inside a fragment in android

I have a requirement in my application. my application has 6 fragments in a Navigation drawer each fragment represents single item in navigation drawer list.
But in one of the fragment i need to implement a form which will be spread across 5 views one at a time, can any one suggest me how to approach this in android.
Initially i thought of doing this using ViewPager , but i dont need horizontal swiping but need to go to next screen when clicked on a button.
Kindly advice me how to approach this.
Thanks & Regards.
Nagendra
You can create main fragment and sub-fragments. Inside the main fragment, show the sub-fragments by replacing them, store the data by creating an entity object here and pass it to the sub-fragments. Inside sub-fragments, pass the data to the main fragment entity using listeners.

Handling multiple instances of same fragment - android?

Scenario :
I need to create tabs/with fragment, and populate views inside each fragments dynamically. Also user can navigate inside each fragment depending on the server response.
Now what I am trying to do is setting same fragment for all tabs and passing different model data according to the tab content. Its working. When i need to show a details page inside a tab again I am showing same fragment with new data. but it makes issues (view mix up) on other tabs since the container is same.
Can anyone suggest a flow by using single fragment for any new views and i need to pop it out from backstack also.

Android: Different fragment for each ViewPager

In Android, when creating Action Bar Tabs with ViewPager, what's a way of giving different fragment for each ViewPager? Let's say I want the first tab to be login form, the second a signup, in fragment_login.xml and fragment_signup.xml files respectively.
Where/how do I initialize these fragments and show as appropriate tabs are selected? I would prefer to do this all in one Fragment class, instead of creating individually for each one.
If you are using a small number of fragments then you can implement FragmentPagerAdapter. For showing larger number of fragments FragmentStatePagerAdapter is recommended. You can keep all the fragments in one class(Activity class) and make each fragment a subclass of that class but I think having different classes in respective .java files would make your code more elegant.
Initializing the fragment is generally done during fragmentTransaction and appropriate data are passed via Bundle.

Android Tabs, Detail View on tablet

I'm currently designing the following 2 pane layout and I'm wondering on how to implement it.
Link to Wireframe
So far, I made a two pane activity. On the left is a custom ListView fragment. On the right is a detailpane. The detail pane is what I'm having trouble implementing. I don't know how to create a tab view that doesn't use the actionbar and I'm curious if the best way is to use a ton of fragments for the detailpane or just add change the data dynaically.
[The reason I don't want to use the action bar is that I'm using it for my Help/Refill buttons]
So what tools/tutorials/advice can anyone recommend in implementing this?
Additional information:
Categories or the Tabs will be based on a JSON array.
The listview inside the tabs will be filled with items from a JSON array.
Let me know if you need any more information.
I am currently using a FragmentTabHost and it's just perfect!
You can have your ListFragment and a simple Fragment with your details.
And use your FragmentActivity to communicate with them.
You just need to override the onAttach method of your activity which is called when a Fragment is displayed.

Categories

Resources