简体   繁体   English

导航抽屉单击以打开另一个片段来替换TabLayout和ViewPager

[英]Navigation Drawer click to open another fragment to replace TabLayout & ViewPager

I have a project that has a navigation drawer and it includes with 2 item "Home" and "Message". 我有一个带有导航抽屉的项目,它包含2个项“ Home”和“ Message”。 "Message" page is a individual fragment and "Home" page is a tabLayout combine viewPager that includes 3 different fragment. “消息”页面是一个单独的片段,“主页”页面是一个包含3个不同片段的tabLayout组合viewPager。 What I want is "Message" fragment to replace Home's tabLayout and viewPager. 我想要的是“消息”片段来替换Home的tabLayout和viewPager。 Now my problem is when I try replace with "Message" fragment, the fragment itself didn't show anything but considering there is big TextView inside the "Message" fragment. 现在我的问题是,当我尝试用“消息”片段替换时,该片段本身什么也没显示,但考虑到“消息”片段中有很大的TextView。

MainActivity's navigation click code 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;
        }
    });

Image of Home tabLayout 主页tabLayout的图像

“家”的照片

Image of Message fragment, this fragment suppose to have a textView in the middle Message片段的图像,该片段假定中间有一个textView

“ Message”的照片

I try to use getSupportFragmentManager() to replace my TabLayout but it seems not working at all. 我尝试使用getSupportFragmentManager()替换我的TabLayout,但似乎根本不起作用。 Does anyone has any Idea of what am I doing wrong or what am I lacking in this code, I have been struggling these days to solve this problem... 有人对我在做什么错还是在这段代码中缺少什么有任何想法,这些天我一直在努力解决这个问题...

Update MainActivity XML code 更新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" />

when it is only two items, you could use a ViewFlipper , which permits two child nodes. 当只有两个项目时,可以使用ViewFlipper ,它允许两个子节点。

or use a FrameLayout as a container and inflate fragments accordingly, instead of pre-existing XML . 或者将FrameLayout用作容器并相应地填充片段,而不是预先存在XML

as it is, it would inflate the second fragment, with no chance to return to the first one fragment. 实际上,它将使第二个片段膨胀,而没有机会返回第一个片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM