繁体   English   中英

当父级不是活动而是片段时为空选项卡

[英]Empty tab when parent is not an activity but a fragment

我有一个正在运行的带有导航抽屉活动的 Android 应用程序。 对于每个菜单项,我想实现一个单独的片段,这样我就可以使用相同的工具栏和抽屉菜单。 现在,其中一个片段应该包含一个带有 3 个选项卡的视图(其中包含 3 个带有 Recyclerviews 的片段)。 我创建了一个选项卡式活动并将代码迁移到一个新的片段中。 当我第一次单击带有 Tab-Fragment 的菜单项时,它工作得非常好。 但是当我浏览菜单然后再次打开 Tab-Fragment 时,它显示了 3 个选项卡,但其中没有任何内容。 现在,当我单击第三个选项卡时,将显示数据。 当我切换回第一个选项卡时,数据也回来了。 只有中间的 Tab 保持空白。

我在一个全新的空项目上尝试了这种方法,只有一个抽屉式导航活动和一个选项卡式活动,我还将代码迁移到一个片段。 我只使用了 Android-Studio (3.4.2) 生成的代码。 在这些选项卡中只有文本视图,但仍然存在同样的问题。

Tab-Fragment(从生成的选项卡式活动中迁移的代码):

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.activity_tab, container, false);

        context = getActivity().getApplicationContext();

        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(context, getActivity().getSupportFragmentManager());
        ViewPager viewPager = v.findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = v.findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);

        return v;
    }

生成的 SectionsPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    @StringRes
    private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2};
    private final Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return "Some Title";
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

我只更改了“getCount”中的选项卡数量和“getPageTitle”中返回的字符串

我使用 NavigationItemSelectedListener 处理导航抽屉活动(主)中的菜单项单击,并像这样更改片段:

getSupportFragmentManager().beginTransaction().replace(R.id.content, new TabFragment()).commit();

“activity_tab”布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
</android.support.design.widget.CoordinatorLayout>

是否有可能在菜单项点击时保留片段,还是我必须使用选项卡式活动?

首先,我认为你应该使用 childFragmentManager 而不是 supportFragmentManager。

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(context, getChildFragmentManager());

暂无
暂无

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

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