将片段替换为包含ViewPager

时间:2016-11-23 05:45:01

标签: android android-fragments android-viewpager

我有一个调用FragmentParent的活动。这包含一个viewpager。 viewpager有2个标签。在每个标签中都有一个片段。在第一个选项卡(TAB-A)中,有一个包含Recyclerview的片段。当我点击一个项目时,应用程序会打开新的片段,但是当我返回时,viewpager中的所有片段都会变空。

我正在使用Fragments和支持库v-13,我已经尝试过使用v-4,但我遇到了同样的问题。

这是我的代码:

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_test);

    initWithFragments(savedInstanceState);
}

public void initWithFragments(Bundle savedInstanceState) {

    if (findViewById(R.id.fragment_container) != null) {

        if (savedInstanceState != null) {
            return;
        }
        // Create a new Fragment to be placed in the activity layout
        MainFragment mainFragment = new MainFragment();
        //Fragment transaction
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragment_container, mainFragment);
        ft.commit();
    }
}

MainFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_app, container, false);

    //Enabled menu
    setHasOptionsMenu(true);

    // Initialize Toolbar and View Pager
    toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

    // Create the adapter that will return a fragment for each section
    SectionsPageAdapter mSectionsPagerAdapter = new SectionsPageAdapter(getFragmentManager());

    // Set up the ViewPager with the sections adapter.
    ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.container_view_pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            switch (position) {
                case 0:
                    toolbar.setTitle("Home");
                    break;
                case 1:
                    toolbar.setTitle("Downloads");
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    // Set up tab layout
    TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    return rootView;
}

fragment_app

<android.support.design.widget.CoordinatorLayout 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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

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

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>

这是viewpager中的FragmentA

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGrayLight"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<LinearLayout
    android:id="@+id/container_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/video_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

我也有PageAdapter

public class SectionsPageAdapter extends FragmentPagerAdapter {

private int NUM_PAGES = 2;

public SectionsPageAdapter(FragmentManager fm) {
    super(fm);
}

@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).
    switch (position) {
        case 0:
            return SearchFragment.newInstance();
        case 1:
            return DownloadFragment.newInstance();
        default:
            return new Fragment();
    }
}

@Override
public int getCount() {
    return NUM_PAGES;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Search";
        case 1:
            return "Downloads";
    }
    return null;
}

}

我正在使用FragmentTransaction替换Fragments ...

FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container,settingFragment);
    ft.addToBackStack(null);
    ft.commit();

如果你们能帮助我,我将非常感激 感谢

这就是我想要的:

enter image description here

2 个答案:

答案 0 :(得分:1)

更改FragmentA布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorGrayLight"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:id="@+id/container_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/video_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</FrameLayout>

现在用这个

替换片段
FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container,settingFragment);
    ft.addToBackStack(null);
    ft.commit();

R.id.fragment_container

需要FragmentA

答案 1 :(得分:0)

我最终解决了使用getChildFragments:

我使用了getChildFragmentManager而不是getFragmentManager。我传递给PageAdapter的构造函数

SectionsPageAdapter mSectionsPagerAdapter = new SectionsPageAdapter(getChildFragmentManager());

在RecyclerView的视图持有者中,我创建了一个接口,用于将嵌套片段与Activity(主容器)进行通信。使用这种方式,我可以替换R.id.fragment_container

中的Fragments

我在此链接中找到了解决方案: Adding child Fragment to Parent Fragment withing a ViewPager in Android