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.
Related
I googled this answer but didn't find something for me.
I want to use fragments (two different fragments) as items on RecycleView.
If you know how I can do it please help me.
Thanks.
Create an interface and use it as your generic type of your RecyclerView and Adapter. Your item's layout will be this interface. And finally your fragments implements this interface.
Note: Fragments may cause irregularity since they are attached activity. Not preferred!
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
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.
I have fragments in a FragmentActivity, that has an ActionBar and Tab navigation. My problem is that I want to call a method on the Fragment. I cannot use findFragmentBy[Id|Tag], because the fragments are initiated and attached by the adapter, and it assigns a custom tag. I saw several solution based on keeping references to fragments, but I really want to avoid keeping any reference to my fragments, as they are handled by the FragmentPagerAdapter. Now I have everything in my Fragment (which is a workaround in my view), but the question still bothers me.
What is the best way to access a fragment instance from my activity, not added by my to FragmentManager, but the FragmentPagerAdapter?
I solved this problem by writing my own Adapter. Since they are pretty straightforward, they are easy to be implemented.
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.