Currently, my app is using a navigation drawer for my fragments. However, I have 2 fragments that are related, I would like to put these under one heading on the navigation drawer and then have 2 tabs to switch between the related fragments once I select that choice on the drawer.
Is this possible with only one activity or would I have to implement another activity specifically for the tab? I've considered only adding the tabs once the right fragment is committed but that leaves the question of how to remove it once I switch to a fragment that doesn't require the tabs.
I'd also like to avoid having to implement another activity as I am trying to keep the navigation available through my whole app. I'd rather not have to create an identical drawer and then have to keep things consistent between the 2 drawers.
Edit:
So far, I've managed to make a parent fragment that has a viewpager, fragmentpageradapter, and tablayout. Swiping between the 2 tabs does indeed shift between my 2 desired child fragments. However, I am unable to change a textview in one of my child fragments from my MainActivity. I'm guessing the reason why this is the case is because technically, my parent fragment is in view, not my child fragment. Any suggestions to get around this?
Hierarchy:
MainActivity
-Navigation Drawer
--Fragment1
--Fragment2
--Fragment3
--Fragment4
---FragmentPagerAdapter (under Fragment4, tabs)
----SubFrag1
----SubFrag2
Here's how you might use FragmentStatePagerAdapter to swipe across a Fragment:
http://developer.android.com/intl/es/training/implementing-navigation/lateral.html
I hope it help you.
Related
I have a fragment with a textView. I want the textView to change when I scroll across the viewPager.I want to reuse the same fragment rather than creating a new fragment for displaying each text.How do I do this? I hope pro developers can understand what I mean.
You cannot attach a same fragment into activity twice simultaneously. If you want the behavior you mentioned don't use ViewPager. Just use swipe gesture detection in a layout and maybe some animations.
I have recently decided to add a Navigation Drawer to every screen in my app. I already have the working code. Is there a faster way to add the same layout to every screen or must I do this manually?
One way you could do it is to create an Activity or Fragment class say BaseActivity that all your Activity or Fragment will inherit, and put your Navigation Drawer logic there.
I followed this tutorial about navigation drawer androidhive-navigation drawer and it has used fragments for the list items.
Similarly all sources I found use fragment views for individual list items. I have these issues.
Why use fragments, when we can use an ordinary Activity class?
Going with the link example, if I have Home Activity class implemented before, what should I do to convert it to a fragment?
If Home is the main activity, what happens then?
Im working with a Android app that uses Navigation Drawer. This app use the template provided with Eclipse ADT (when you select the navigation drawer template).
I dont understand the behaivour of the navigation drawer very well.
My main requeriment is make a "search" option, I have on the Navigation Drawer a EditText to get the query string from the user. I want that I press search button and open my SearchFragment getting the search query.
I know that I can make this:
Bundle args = new Bundle();
args.putString("searchQuery",searchQuery);
fragment.setArguments(args);
And this for getting:
getArguments().getString("searchQuery");
But I have the next:
MainActivity
NavigationDrawerFragment
SearchFragment
OtherFragments.. (I think this arent relevant in the question)
I dont understand where I can make this steps.
More data:
I have in the navigation drawer class the EditText with the setOnEditorActionListener for the search press.
Any info that I can add I will be add. Sorry my english
Thanks for the help
Navigation drawer is basically a component that show 2 pane in the way that one is the main pane (usually fragment) that is used to show content and the other one is usually a listview that is just used to choose which fragment to show in main pane
so if you want to switch between 2 different layouts you have to create 2 different fragments and then you can add or replace these fragments based on listview's selected item.
You may refer to below link for complete code
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Hope it helps!
In my main activity I have a ViewPager with two tabs and each of them has its contents in a fragment. One of the tabs holds a list of items and when I press on an item from that list I want the details of that item to appear on the screen. I thought of putting the whole ViewPager in a fragment and when the user taps the item the fragment representing the whole ViewPager is replaced with another fragment representing the details of the item. Is it possible and is it a good practice? Maybe I should just start another activity with the details of the item?
How you do it is your own choice. It's perfectly fine to switch your Fragments.
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FragDetails())
.addToBackStack(FragPager.class.getName()).commit();
Switching to another Activity is fine too, you just have more of a "flicking" screen when opening a new Activity (and some other life cycle stuff too of course). If you don't want that, switch Fragments.