Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment inside a ListFragment

I am using a custom viewpager which has Listfragment's loaded with FragmentPagerAdapter. Each ListFragment represents a dual panel layout, where each list item loads some other fragments to a framelayout(Right Panel).

Is this a best practice?

like image 928
Raneez Ahmed Avatar asked Dec 29 '25 06:12

Raneez Ahmed


2 Answers

simple answer is...

you cannot add fragment inside a fragment.

UPDATE

instead what you can do is... use PagerAdapter and set the view inside which has two fragments.

Brief

PagerAdapter is parent class of FragmentPagerAdapter so you need to make adapter which extends the pageradapter refer this link you will have an idea,

you need to look @ the instantiateItem, This method is responsible to generate the view, so you need to inflate and return the view in which there are two fragments.

I don't have working example right now but you can achieve this by PagerAdapter

like image 166
Nixit Patel Avatar answered Dec 31 '25 20:12

Nixit Patel


The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
like image 28
Raneez Ahmed Avatar answered Dec 31 '25 21:12

Raneez Ahmed