如何移动到导航片段?

时间:2019-10-09 18:52:01

标签: android kotlin fragment navigation-drawer

我正在使用侧面导航和底部导航创建一个应用程序。现在MainActivity的布局是抽屉式布局。其中有一个侧面导航,viewpager和一个底部导航。 现在,此viewpager与底部导航链接。并且工作正常。 现在为侧面导航片段编码,片段必须与活动链接,但正如我已经尝试将其与MainActivity链接一样,但它没有响应。我也尝试过创建另一个活动,并使用MainActivity中声明的抽屉布局,但是这样做也不起作用。 这是我的代码

xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">



    <com.google.android.material.navigation.NavigationView
            android:id="@+id/side_nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="false"
            android:background="@color/side_nav"
            app:headerLayout="@layout/nav_header_main"
            app:itemTextColor="@color/white"
            app:menu="@menu/side_nav_menu" />

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <include
                android:id="@+id/appBarMain"
                layout="@layout/app_bar_main" />

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewpager_dashboard"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/bottomTabLayout"/>


            <com.google.android.material.tabs.TabLayout
                    android:id="@+id/bottomTabLayout"
                    android:layout_width="match_parent"
                    app:tabSelectedTextColor="@color/colorPrimary"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_bottom_nav_background"
                    android:layout_alignParentBottom="true">

            </com.google.android.material.tabs.TabLayout>

        </RelativeLayout>

    </LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.side_nav_activity)

        doInitialSetup()

        val toolbar: Toolbar = findViewById(R.id.toolbarSideNav)
        setSupportActionBar(toolbar)


        /*
              SIDE NAV
         */
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val sideNavView: NavigationView = findViewById(R.id.side_nav_view)
        val toggle = ActionBarDrawerToggle(
            this,
            drawerLayout,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        sideNavView.setNavigationItemSelectedListener(this)
    }


    override fun onBackPressed() {
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }


    /*
       for SIDE NAV
     */

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_makePayment -> {

            }
            R.id.nav_maps -> {

            }
            R.id.nav_food_n_beverages -> {

            }
            R.id.nav_team -> {
                val teamFragment = FragmentTeam.newInstance()
                openFragment(teamFragment)
            }
            R.id.nav_developer -> {

            }
            R.id.nav_sponsors -> {

            }
            R.id.nav_emergency -> {

            }
        }
        val drawerLayout: DrawerLayout =
            findViewById(R.id.drawer_layout)
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

    /*
        Fragment Transaction -- for fragment transaction, that allows you to use intent feature for fragment
    */


    private fun openFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

0 个答案:

没有答案