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.
Related
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.
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’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.
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
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.