Android Drawer Navigation with a ListView, need explanation - android

I have implemented a basic Navigation Drawer.
I understand Android Drawer Navigation utilizes Fragment (Nav1 opens Frag1, Nav3 opens Frag3).
I want to achieve Frag1 (or the Home Activity) as a list (i.e., ListFragment) And, when an object on the list is clicked it will take me to Frag4....Frag10 depending on what is clicked.
In another term,
Drawer Menu (Nav1...Nav3)
Home Activity (Could be Frag1) is a list and a swipe tab << This is where I am stuck.
Other Activities are Swipe tabs too...
Any suggestion, close example?
Thanks

OK, I have found the answer, a very simple solution. Create ListFragment class. And in the main class just receive the list!
I have taken help from ListFragment

Related

Switch tab on button click with Bottom Navigation and Navigation component

I have a very simple app that consists of three Fragments and a Bottom Navigation bar, created by using "New Project -> Bottom Navigation Activity" in Android Studio. The first Fragment holds a button, which should take me to the second Fragment, like if the middle button of the Bottom Navigation bar was clicked.
Is there a "standard" way to do this?
I have tried:
using launch(...) of the Navigation component, which seems to launch the Fragment with its own back stack and breaks the bottom navigation.
using setSelectedItemId(...) in different ways, which either results in an exception or breaks bottom navigation in different ways.
In this post, someone asks the exact same question, but it is marked as a duplicate. I fail to find the answer, especially concerning Navigation component.
Clicking the Button should have the same effect as if the user taps the corresponding item in the bottom navigation. So you need to call setSelectedItemId() on the BottomNavigationView. This can only be done in the Activity displaying the BottomNavigationView.
One option is to introduce a shared ViewModel with
a LiveData to be observed by the Activity
a function onButtonClicked() to be called by the OnClickListener of your Button which will update the LiveData
Once the LiveData observer fires, your Activity can call
binding.navView.selectedItemId = R.id.navigation_dashboard
Please note that for passing information about events like this one should choose some data type which can be invalidated after use. See for example LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)
Paste this code from where you want to go to the second fragment
Fragment fragment = new DashboardFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
For more information click here

Using ViewPager and PagerTabStrip - extending FragmentActivity

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.

Android project code structure for app - deailing with 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.

Proper way to configure ActionBar when using Fragments

I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.
I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

Navigation drawer with fragments and only one activity

Im doing an app with navigation drawer.
For this, i have a HomeActivity, this contains all the login of my navigation drawer, the options in menu, the view, the titles etc. And here, i set listenerclick for navigation elements.
This listener receives FragmentManager and with a switch do:
smf.beginTransaction().add(R.id.frame_content, new Fragment()).commit();
Replacing fragment for the fragment that i need in each case of switch.
In home layout i have a framelayout and navigation drawer.
Mi question is, is correct that i only have one activity with a framelayout, and depends on the item clicked in navigation drawer i replace the fragment on the frame, or is better have lot of activitys, and create menu in all of them with the same login, and when user click in item menu, launch new intent with the activity selected?
I hope i have explained ok...
Thank you.
I did this same thing, but I found it was a lot better to have different activities.
If you do go down the separate activities path you should have one base activity that the activities extend so you don't need to rewrite the drawer code.
A fragment is only really meant to be an extension of an activity, for example when you have multiple tabs, or you are swiping between different views, or you need to break up your activity into sections.

Categories

Resources