我有一个正在运行的Android应用程序,它带有一个NavigationDrawerActivity,我希望为每个菜单项实现一个单独的Fragment,这样我就可以使用相同的工具栏和抽屉菜单了。2现在这个片段应该包含一个有3个标签的视图我创建了一个选项卡式Activity,并将代码迁移到了一个新的片段中。当我点击带有Tab-第一次使用Tab-Fragment时,它运行得非常好。但是当我浏览菜单,然后再次打开Tab-Fragment时,它显示了3个标签,但是里面什么都没有。现在当我点击第三个标签时,数据显示出来了。当我切换回第一个标签时,数据也回来了。只有中间的标签保持空白。
我在一个全新的空项目上尝试了这个方法,只有一个导航抽屉Activity和一个标签Activity,我也把代码移植到了一个片段中。我只使用了Android-Studio(3.4.2)生成的代码。在这些标签中只有文本视图,但仍然存在同样的问题。
选项卡片段(从生成的选项卡式活动迁移的代码):
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;
}
生成的节PagerAdapter:
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处理NavigationDrawer Activity(Main)中的菜单项单击,并将片段更改为如下所示:
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>
是否有可能在菜单项单击时保留片段,或者我必须使用选项卡式活动?
1条答案
按热度按时间o4hqfura1#
首先,我认为您应该使用childFragmentManager,而不是supportFragmentManager。