简体   繁体   English

Android ViewPager片段

[英]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 ). 所以我的实际问题是我有一个活动( 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). 在该活动的布局中,我创建了FrameLayout ,然后在其中附加了不同的四个片段(因此每个片段都extends Fragment 。因此,在其中一个片段中,我想实现可滑动选项卡(请参见屏幕截图)。

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. 我知道通常是通过实现viewPager和FragmentPagerAdapter来完成的,但是我无法做到这一点,就好像我在片段中调用getSupportFragmentManager一样会给我一个错误。 If i am using getActivity().getFragmentManager() it gives me error that android.support.v4.app.fragmentmanager cannot be applied to android.app.fragmentmanager . 如果我使用的是getActivity().getFragmentManager()则会出现错误: android.support.v4.app.fragmentmanager无法应用于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). 我必须在我的片段中使用android.support.v4.app.fragment,因为否则我将无法实现我的活动视图(如开篇所述)。

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. 确保您使用的Fragment类扩展了来自支持库的片段。 You also need to use FragmentActivity to call method getSupportFragmentManager(); 您还需要使用FragmentActivity来调用方法getSupportFragmentManager();

On the other hand, viewpager which is in your fragment need to implemented as usual that you can find on internet except getChildSupportFragmentManager(); 另一方面,片段中的viewpager需要照常实现,您可以在互联网上找到它,除了getChildSupportFragmentManager();

This one called "nested fragments". 这个叫做“嵌套片段”。

PS: I am not sure but you can also use AppCompatActivity instead of FragmentActivity . PS:我不确定,但是您也可以使用AppCompatActivity代替FragmentActivity FragmentActivity and ActionBarActivity must be deprecated . 必须不建议使用 FragmentActivityActionBarActivity

Good luck 祝好运

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

New version of Support Library v4 (Android 4.2) resolve this problem. 新版本的支持库v4(Android 4.2)解决了此问题。 For do this, simply do constructor of your custom FragmentPagerAdapter like this: 为此,只需像这样自定义FragmentPagerAdapter的构造函数即可:

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 这项工作是因为新的Android版本批准使用嵌套片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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