用新片段替换ViewPager

时间:2017-07-03 18:12:56

标签: android android-layout android-fragments android-viewpager android-tablayout

我有一个应用,其中我的MainActivity有一个viewpager和TabLayout有三个片段(片段a,b和c)。我选择了一个菜单项我想打开一个新的单独片段(片段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>

2 个答案:

答案 0 :(得分:0)

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

PS:如何使用Activity代替Fragment

答案 1 :(得分:0)

所以事实证明我可以使用一个活动,而不是片段!