I did so much researches in the internet and the conclusion was that we cannot use BottomNavigationView and ViewPager and TabLayout simultaneously. but there are a lot of app such as Instagram that merged them.
my question is that how can they do that? do they use a special trick or not?
you can create bottomNavigationView in activity and attach fragment in it with this code when click a on item of it:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(frameId, fragment, "fragTransaction");
transaction.commit();
and then create a fragment wiht a tabLayout and viewPager to control child fragment in it and you most using to control fragment in viewPagerAdapter
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(frameId, fragment, "fragTransaction");
transaction.commit();
It is not difficult to use bottomNavigationView with ViewPager.
In your layout declare both of these controls.
Setup viewPageAdapter and add fragments to it, set adapter to viewPager.
Setup bottomNavigationView overriding onNavigationItemSelectedListener, which now will be setting current item of viewPagerAdapter.
Do you need more detailed code examples, or you are ready to rock?
Related
I am looking for a better approach here. What you suggest if i have 10+ fragments on a navigation drawer.
FragmentManager fragmentManager = getSupportFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
Using singleton pattern to get the instance of a fragment and add just once to fragmentTransaction if it's not added.
fragmentTransaction.add (R.id.testLayoutContent, fragment);
Or the another approach which initialize fragment each time and replace the fragment.
NOTE: Each fragments have customize components, live map etc.
I am develop the app using the fragments and i am facing one fragment1 adding to the another fragment2
fragment1 onclicks performing fragment2 .I didn't findout the solution for that. please guide anyone know
I am using the following code to add the fragment
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = activity.getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.container,fragment2);
ft.hide(new Fragment1());
ft.addToBackStack(Fragment1.class.getName());
ft.commit();
The above to use adding onefragment1 adding to fragment2.Fragment1 onclicks perform to fragment2 . i didn't findout mistake.
please guide me anyone know .Advance thanks to all
nested Fragments are not recommended and for adding a fragment to a container from another Fragment use the parent Activity to do so , you can define a function inside Parent Activity which replace current Fragment with your second Fragment and call it from your first.
Adding a fragment to another fragment is the concept of nested fragments and not recommended. You should replace the fragment instead of adding. Use the following:
Fragment fragment = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("fragment").replace(R.id.frame_container, fragment).commit();
I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
I have a actionbar tab setup with fragments for each of the pages. (extended default example from wizard).
I'm attempting to create a new fragment and make it the current fragment, with the other hidden
ConfigDetailSectionFragment configDetailSectionFragment = new ConfigDetailSectionFragment();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_main_config, configDetailSectionFragment);
transaction.addToBackStack(null);
transaction.commit();
the code shows the new fragment but seems it is transparent and I can see the fragment below.
i'm probably forgetting to do something... what am i forgetting to do or am missing?
Adding an android:background="#FFFFFF" fixed the issue, per beworker. Thanks!
I'm using a ViewPager together with a FragmentPagerAdapter to host four different fragments.
What I'm trying to achieve is to successfully replace Fragment1 with a whole new fragment, newFragment,on click of a button.
When I use..
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment1_layout_id, newfragment);
transaction.remove(fragment1);
transaction.commit();
The fragment is replaced and Fragment is shown instead of Fragment1. Though as soon as I swipe all the way to Fragment4 and then back to newFragment, Fragment1 has made a comeback.
Your fragments are provided by FragmentPagerAdapter.
You would need to adapt what fragment your FragmentPagerAdapter creates for your Fragment1 and then call notifyDataSetChanged() on the adapter.
With your approach, the time Fragment1 should be shown again, the ViewPager will simply ask your FragmentPagerAdapter to re-create the specific instance - which will give you back the instance you were trying to replace.
Not that ViewPager can cache fragments internally, as long as notifyDataSetChanged() was not called on the adapter. Hence, it may not pick up chances you have made without that call.