I’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.
Related
I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.
First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.
I've run into a road block with creating a basic app - due to my understanding of fragments and basic app structure. Could you give me an idea for how my app should be structured?
I have an app with a navigation drawer (currently in the main activity).
The main activity layout has the drawerlayout widget, a frame layout (for fragments), and the list view for the drawer.
Each fragment (or class) is selected thru the navigation drawer.
this has worked fine so far, but I have run into a roadblock. I'd like to create a new fragment which would has tabs. Just about every tutorial I've looked at creates an activity that extends FragmentActivity ... this won't work since FragmentActivity is an Activity.
My question is - is the way I'm structuring my app to work wrong? How should I go about implementing this new tabbed activity / fragment?
Please let me know if you need additional details about the app.
You could use a ViewPager with a FragmentStatePagerAdapter, and add a TabStrip on top. The ViewPager can be contained in a separate Fragment, so would meet your requirement.
See this post for further details Display fragment viewpager within a fragment
You might want to consider launching an activity when an item is selected from the navigation drawer. Just call startActivity(your_intent) in the onClick for the navigation drawer item.
This way the new activity can extend FragmentActivity as the tutorials suggest.
I am using Fragment for Slider menu in Android. Source : Android Slider Menu
Now, I want to add ViewPager inside first Fragment which is Home. So I found some tutorials for it. But all are using FragmentActivity so I am not able to call it inside Fragment.
Please help me regarding this.
I think this should help you out.The key is to use Nested Fragment calls instead of a calling an Activity that uses the FragmentActivity.Find more about Nested Fragment calls here Please check out this question and see how this is implemented.
Android: Viewpager inside a fragment of Navigation Drawer
Let me describe my project.
I use actionbarsherlock with SlidingMeno from jfeinstein10 . On my main activity i have this sliding menu with a couple of menu item that open Fragments in container. (the same as basic fragment sample in sliding menu example) In one of those fragments i have view-pager with viewpageindicator. View-pager is tied up to FragmentStatePagerAdapter with my fragments. So i have fragments inside fragments. I use getChildFragmentManager() like suggested in the doc, when using fragments inside fragments.
When i select menu item with fragment that hold view-pager for the first time everything work like it should. But when i select some other menu item and then go back to this view-pager fragment i get this exception.
All other fragments don’t have this problem and in stack trace there is no reference to my code.
https://gist.github.com/4502038
Did any one encounter similar problem or have some idea what is going on.
EDIT: If i use getFragmentManager() instead of getChildFragmentManager() even thou i'm in subfragment. I don't get an exception, but one of the fragments inside viewpager doesn't get recreated after i select viewpager fragment for the second time.
I created a project on github so you guys can take a look.
https://github.com/pzagor2/TestErrorApp
See this: https://code.google.com/p/android/issues/detail?id=42601 bug.
Uncommenting addToBackStack https://github.com/pzagor2/TestErrorApp/blob/master/MainActivity/src/com/example/myapp/MainActivity.java#L69 , worked for me with your project setup.
You can see the bug in the Google bug report in the first URL.
Hope this helped.
I just looked at the GitHub project and it looks like you have not commited your code yet. But I assume your ViewPager is in a fragment itself and the ViewPager is hosting child fragments. If that's so, then I would recommend you test to make sure your FragmentStatePagerAdapter is constructed with the result of getChildFragmentManager().