我有一個應用程序,其中我的MainActivity有一個viewpager和TabLayout與三個片段(片段a,b和c)。我有一個菜單項,當選擇我想打開一個新的單獨的片段(片段d)重疊我的viewpager一個tablayout。當我點擊我的菜單選項片段d出現,但片段a,b或c仍然在後臺。替換ViewPager與新片段
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>
嘿!謝謝你的迴應,我很感激。我更改添加替換和我仍然有同樣的問題。我使用片段而不是活動,因爲我有一個導航抽屜,我想確定導航抽屜中的discoverFragment –