简体   繁体   中英

Android viewpager in fragment

Dear ladies and gents,

First of all, please do not mark my question down. If you think that my question is too stupid please let me know and i will edit it or remove.

So my actual question is that I have an activity ( extends Activity ). In that activity's layout I created FrameLayout where I then attach different four fragments(so each of them extends Fragment . So in one of that fragments I would like to implements swipeable tabs(see screenshot).

I know that it is usually done by implementing viewPager and FragmentPagerAdapter, but I can not do this as if I'm calling getSupportFragmentManager in my fragment it gives my an error. If i am using getActivity().getFragmentManager() it gives me error that android.support.v4.app.fragmentmanager cannot be applied to android.app.fragmentmanager . I have to use android.support.v4.app.fragment in my fragment, because otherwise I will not be able to implement my activity's view(described at the beggining).

Any ideas or suggestions would be very appreciated. screenshot

Make sure you are using Fragment class that you extends your fragments comes from support library. You also need to use FragmentActivity to call method getSupportFragmentManager();

On the other hand, viewpager which is in your fragment need to implemented as usual that you can find on internet except getChildSupportFragmentManager();

This one called "nested fragments".

PS: I am not sure but you can also use AppCompatActivity instead of FragmentActivity . FragmentActivity and ActionBarActivity must be deprecated .

Good luck

在其他Fragment中使用Fragment时,可以使用getChildFragmentManager()。

New version of Support Library v4 (Android 4.2) resolve this problem. For do this, simply do constructor of your custom FragmentPagerAdapter like this:

public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
{
    super(fragment.getChildFragmentManager());

    // write your code here
}

This work because new Android version approve using nested Fragments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM