SlidingTabLayout寻呼机片段消失父片段的onPause()被调用后, [英] SlidingTabLayout Pager fragments are vanishing after parent Fragment onPause() is called

查看:191
本文介绍了SlidingTabLayout寻呼机片段消失父片段的onPause()被调用后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用设计,如:A,B,C和D片段

My App is designed like: A, B, C and D fragments.

|  A  |  B  |
         ↓
   |  C  |  D  |

A和B是顶级Tablayout( TL1 )片段。我还有一个Tablayout( TL2 )属B持有C和D。

A and B are top level Tablayout(TL1) fragments. I have another Tablayout(TL2) in B which holds C and D.

TL1和TL2都工作得不错,我第一次打开应用程序。
一段时间后,当我切换到片段A和回来B,我只能看到TL2标签的标题,但内容片段缺失。

Both TL1 and TL2 work fine the first time I open the App. After sometime, when I switch to fragment A and come back to B, I can only see TL2 tab headings but the fragment contents are missing.

我没有看到与TL1这种行为是主要活动的一部分。我怀疑有东西在onPause()B片段调用和返回回来的时候是不能够找到TL2的内容。

I dont see this behaviour with TL1 which is part of main activity. I am suspecting something to do with onPause() call of fragment B and when returning back it is not able to find the contents of TL2.

我的片段XML是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<com.example.rv.android.ui.widget.SlidingTabLayout
    android:id="@+id/slidingProfileTabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"/>

<android.support.v4.view.ViewPager
    android:id="@+id/vpProfileTab"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white"/>

我设立的标签在我片段 onCreateView()

I am setting up the tabs in my fragment onCreateView() like

    private void setupTabs() {

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    vpProfileTab.setAdapter(new MyProfileTabFragmentPagerAdapter(getActivity()
            .getSupportFragmentManager(),
            ctx));

    // Give the TabLayout the ViewPager
    slidingProfileTabs.setDistributeEvenly(true);
    slidingProfileTabs.setBackgroundColor(colorAccent2);
    slidingProfileTabs.setSelectedIndicatorColors(colorTabIndicator);
    slidingProfileTabs.setViewPager(vpProfileTab);
}

每个标签都在自己的碎片。我的页面适配器是:

Each of the tabs are fragments in themselves. My page adapter is:

public class MyProfileTabFragmentPagerAdapter extends FragmentPagerAdapter {

private String tabTitles[];
private Context context;
private static final String TAG = makeLogTag(MyProfileTabFragmentPagerAdapter.class);

public MyProfileTabFragmentPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    tabTitles = context.getResources().getStringArray(R.array.profileTabs);
    this.context = context;
}

@Override
public int getCount() {
    return tabTitles.length;
}

@Override
public Fragment getItem(int position) {
    LOGI(TAG, "Info tab" + position);

    switch (position){
        case 0:
        {

            return new UserProfileFragment();
        }
        case 1:
        {

            return new FeedFragment();
        }

        default:
        {
            LOGI(TAG, "Invalid tab" + position);
            return null;
        }
    }
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}}

如何继续前进有什么建议?

Any suggestions on how to proceed forward?

推荐答案

在初始化你的 ViewPager ,而不是的getActivity()。getSupportFragmentManager() ,请尝试使用片段 getChildFragmentManager()方法。

When initializing your ViewPager, instead of getActivity().getSupportFragmentManager(), try using the fragment getChildFragmentManager() method.

这篇关于SlidingTabLayout寻呼机片段消失父片段的onPause()被调用后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆