用新片段替换 ViewPager

Replacing ViewPager with new fragment

我有一个应用程序,其中我的 MainActivity 有一个 viewpager 和带有三个片段(片段 a、b 和 c)的 TabLayout。选择时我有一个菜单项我想打开一个新的单独片段(片段 d)以重叠我的 viewpager 和 tablayout。当我单击我的菜单选项时,片段 d 出现,但片段 a、b 或 c 仍在后台。

What is happening

What I want to do

MainActivity.Java

 public class MainActivity extends AppCompatActivity  {

    private DrawerLayout mDrawer;
    private Toolbar toolbar;
    private NavigationView nvDrawer;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ActionBarDrawerToggle drawerToggle;
    private static String TAG = "MainActivity";

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

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        BookShelfPagerAdapter bookShelfPagerAdapter = new BookShelfPagerAdapter(getApplicationContext(), getSupportFragmentManager());
        viewPager.setAdapter(bookShelfPagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_book_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            case R.id.action_discover:
                Fragment discoverFragment = new DiscoverFragment();
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .add(R.id.flContent, discoverFragment)
                        .addToBackStack(null)
                        .commit();

            return true;
        return super.onOptionsItemSelected(item);
    }
}

XML布局:MainAcitvity

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- This LinearLayout represents the contents of the screen  -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- The ActionBar displayed at the top -->
            <include
                layout="@layout/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <!-- The main content view where fragments are loaded  -->


            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </LinearLayout>
    </FrameLayout>

    <!-- The navigation drawer that comes from the left --><!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

将此行 .add(R.id.flContent, discoverFragment) 更改为 .replace(R.id.flContent, discoverFragment)

PS:用 Activity 代替 Fragment 怎么样?

原来我可以使用 activity 而不是片段!