我希望每次从BottomNavigationView打开活动时都打开一个抽屉

时间:2019-05-29 19:41:40

标签: android kotlin bottomnavigationview drawerlayout onbackpressed

我希望每次从BottomNavigationView中打开活动时都打开一个抽屉,类似于工具栏导航菜单。我打开活动后,抽屉打开了。然后我滑动抽屉以关闭并立即从BottomNavigationView中再次打开活动,但是抽屉没有再次打开。我必须按下设备后退按钮,然后再次按下BottomNavigationView项目。

MoreActivity.kt

import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat
import com.google.android.material.navigation.NavigationView
import androidx.navigation.ui.NavigationUI
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.more_activity.*
import kotlinx.android.synthetic.main.app_bar_main.*

class MoreActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.more_activity)
        setSupportActionBar(toolbar)

        val navController = Navigation.findNavController(this, R.id.nav_host)

        val drawer: DrawerLayout = findViewById(R.id.drawer_layout)
        drawer.openDrawer(Gravity.LEFT)

        val navView: BottomNavigationView = findViewById(R.id.bottomNavigationView)

        setupBottomNavMenu(navController)

        setSupportActionBar(toolbar)
        val actionbar: ActionBar? = supportActionBar
        actionbar?.apply {
            setDisplayHomeAsUpEnabled(true)
            setHomeAsUpIndicator(R.drawable.ic_menu)
        }

        setupDrawerToggle()

        Toast.makeText(this, "More Capabilities", Toast.LENGTH_LONG).show()

        NavigationUI.setupWithNavController(nav_view, navController)

    }

    private fun setupDrawerToggle() {
        val mDrawerToggle =
                object : ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.open, R.string.close) {}
        drawer_layout.addDrawerListener(mDrawerToggle)
        mDrawerToggle.syncState()
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    private fun setupBottomNavMenu(navController: NavController) {
        bottomNavigationView?.let {
            NavigationUI.setupWithNavController(it, navController)
        }
    }

}

more_activity.xml

<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">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MoreActivity">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/nav_host"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/nav_graph"
            app:defaultNavHost="true" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:labelVisibilityMode="labeled"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu_5" />


    </FrameLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        android:isScrollContainer="true"
        android:saveEnabled="true"
        android:scrollY="1dp"
        android:scrollbars="none"
        android:visibility="visible"
        app:elevation="2dp"
        app:headerLayout="@layout/nav_header_main_warrior"
        app:insetForeground="@color/colorPrimaryDark"
        app:itemIconTint="@color/colorPrimaryDark"
        app:menu="@menu/drawer_view" />

</androidx.drawerlayout.widget.DrawerLayout>

0 个答案:

没有答案