FragmentTransaction.repalce with customized fragments - android

I am trying to build an activity that employs a ViewPager (FragmentPagerAdapter). Inside the ViewPager, I would like to open fragments programmatically. I know this can easily be achieved using FragmentTransaction.replace(int, Fragment), but I would like to use my own Fragment object. I know you can do that if you use android.app.FragmentTransaction.replace, but unfortunately from what I have found so far you can only use the support package (android.support.v4.app.FragmentTransaction) because the ViewPager originates in the support package.
Are there any workarounds/alternatives? Is there something like ViewPager in Android.view or something? Or anything that can produce tabs at the top and have it swipeable like the ViewPager (FragmentPagerAdapter)?
Really I guess the question I'm asking is if there is a way to use my own Fragment (e.g. MyFragment extends Fragment and use "MyFragment" as a fragment in a ViewPager).
Thanks for the help in advance!

use the support library and use the FragmentActivity class along with the support Fragment class android.support.v4.app.Fragment to use fragments in your viewpager
you will have to change all your fragments you want to use in the viewpager over to the support fragment

Related

TabView inside fragment Android

I want to make tabView inside a fragment. I have template from this and I want to place tabView inside one fragment. Can I implement this like implementation using activity? Any tutorial or advice about this case? Thank you.
Yes. You can use ViewPager and TabLayout
(see http://developer.android.com/training/animation/screen-slide.html)
There are two ways to add the tabs inside fragment:
TabLayout: Using this layout you can add tab in your fragment and also latest approach provided by android.
Tabhost- Using old tabhost layout. This approach is a depreciated approach. However it will also work with fragment properly.
For more reference you can check my example on GitHub-Material Navigation

ViewPager inside a Fragment

Is it possible to inflate a ViewPager inside a Fragment which is inflated on a Activity? Coz almost all examples that a found is a ViewPager inside a FragmentActivity and i dont want to use FragmentActivity (for some reason). It will be looked like this photo :
Please help me, thank you :)
Yes, it is possible, but you have to remember the FragmentManager used inside a fragment to manage child Fragment (for example, the one provided to your FragmentPagerAdapter in its constructor) is obtained with getChildFragmentManager and NOTgetFragmentManager. That is very important, your app will crash if you don't.
Have a look at this tutorial for an example implementation.

Adding a Tab with Fragments inside a Tab Fragment

I know my questions sounds like a tongue twister. Anyway I wanted to implement it but I don't have any idea on how to do it. I already know on how to use a FragmentActivity and add the TabHost for it but now what I have is a fragment that should also contain a Tab which also has a fragment. To give it a more detail here's what I wanted to implement:
What I tried so far is to Create a new FragmentActivity which will be contained inside a FragmentActivity and I got a ClassCastException. Any ideas on how can I achieve this?
Using a newer version of the Support Library, or native Fragments if you only care about Honeycomb, you can add Fragments as children of other fragments.
You can call getChildFragmentmanager() inside one of your top level Fragments and add your child Fragments there.

Fragment inside a fragment with ViewPager

Is it possible to put a fragment inside a fragment in Android? Consider this: Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
Is it possible to put a fragment inside a fragment in Android?
Using the Android Support package's backport of fragments, yes. Also, native fragments on Android 4.2 and higher (API Level 17+) support nested fragments. However, native fragments from API Level 11-16 do not.
Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
This sample project demonstrates having fragments in a ViewPager and having the ViewPager itself be in a fragment. The key is that the FragmentManager you supply to your FragmentPagerAdapter must be the child fragment manager of the outer fragment (i.e., getChildFragmentManager()).

A bug in ViewPager using it with other fragment

I need your help.
I have one Activity with two fragments: one fragment with simple TextView in LinearLayout and other fragment: ViewPager with 3 fragments in FragmentPagerAdapter.
I make transaction with replace action, but I have error from ViewPager: java.lang.IllegalStateException: Recursive entry to executePendingTransactions.
That's a source code that show this problem, maybe anybody knows how to fix it.
Um no. A ViewPager extends ViewGroup. How does that make it a Fragment?
Yes, you're right that nesting Fragments isn't supported. However, it works if you have a Fragment holding a ViewPager whose Adapter contains several Fragments. Trust me, it works. I'm using it in the current project I'm working on right now and I've even got nested ViewPagers without the horizontal touch events fighting for control, so essentially I have Fragments within a Fragment.
With revision 11 of the Android Support Library, you can now nest fragments within fragments to avoid the recursive exception. See this question How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)
A ViewPager is basically a Fragment, and you can't put Fragments in Fragments. Period.

Categories

Resources