[英]How to replace fragment to another fragment in TabLayout and ViewPager
[英]Navigation Drawer click to open another fragment to replace TabLayout & ViewPager
我有一個帶有導航抽屜的項目,它包含2個項“ Home”和“ Message”。 “消息”頁面是一個單獨的片段,“主頁”頁面是一個包含3個不同片段的tabLayout組合viewPager。 我想要的是“消息”片段來替換Home的tabLayout和viewPager。 現在我的問題是,當我嘗試用“消息”片段替換時,該片段本身什么也沒顯示,但考慮到“消息”片段中有很大的TextView。
MainActivity的導航點擊代碼
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case 0:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.Home:
getSupportActionBar().setTitle("Home");
viewPager.setAdapter(mSectionPageAdapter);
viewPager.setCurrentItem(1);
mTabLayout.setupWithViewPager(viewPager);
break;
case R.id.Message:
mTabLayout.removeAllTabs();
getSupportActionBar().setTitle("Message");
getSupportFragmentManager().beginTransaction().replace(R.id.viewPager_mainActivity, friendMessageFragment).commit();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
主頁tabLayout的圖像
Message片段的圖像,該片段假定中間有一個textView
我嘗試使用getSupportFragmentManager()
替換我的TabLayout,但似乎根本不起作用。 有人對我在做什么錯還是在這段代碼中缺少什么有任何想法,這些天我一直在努力解決這個問題...
更新MainActivity XML代碼
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivityUsed.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/add"
app:backgroundTint="@color/backgroundOrange" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="beginning"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="@+id/app_bar_info_regis_main_activity"
layout="@layout/app_bar_info_regis" />
<include android:id="@+id/app_bar_search_view"
layout="@layout/search_view"/>
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_mainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/appbar_main_layout">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="false"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@color/colorAccent"
app:menu="@menu/main_activity_menu" />
當只有兩個項目時,可以使用ViewFlipper
,它允許兩個子節點。
或者將FrameLayout
用作容器並相應地填充片段,而不是預先存在XML
。
實際上,它將使第二個片段膨脹,而沒有機會返回第一個片段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.