Android Kotlin在某些片段中显示导航抽屉图标而不是“后退按钮" [英] Android kotlin show Navigation Drawer icon instead of Back Button in some fragment

查看:129
本文介绍了Android Kotlin在某些片段中显示导航抽屉图标而不是“后退按钮"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某个片段上显示导航抽屉图标而不是后退"按钮.我创建了带有导航图的应用程序.

I want to show navigation drawer icon instead of Back button on a certain fragment. I created the App with Navigation graph.

假设我有2个片段(LoginFragmentDashboardFragment)和一个活动(MainActivity)

Let's say i have 2 fragments (LoginFragment and DashboardFragment) and one activity (MainActivity)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        drawerLayout = binding.drawerLayout

        val navController = this.findNavController(R.id.myNavHostFragment)

        // prevent nav gesture if not on start destination
        navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
            if (nd.id == nc.graph.startDestination) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            } else {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            }
        }

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        NavigationUI.setupWithNavController(binding.navView, navController)

    }


    // Set up the back button on action bar
    override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.myNavHostFragment)

        return NavigationUI.navigateUp(navController, drawerLayout)
    }
}

LoginFragment.kt

class LoginFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding:FragmentLoginBinding = DataBindingUtil.inflate(inflater,
            R.layout.fragment_login, container, false)

        // Hide the Action bar
        (activity as AppCompatActivity).supportActionBar?.hide()

        binding.loginButton.setOnClickListener {
           //Some unimportant validation
        }


        return binding.root
    }

}

DashboardFragment.kt

class DashboardFragment : Fragment() {

    lateinit var binding : FragmentDashboardBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Binding object for this fragment and the layout
        binding = DataBindingUtil.inflate(inflater,
            R.layout.fragment_dashboard, container, false)

        //Navigate to Product stock fragment when clicked
        binding.productStockButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_productStockOutletList
        ))

        //Navigate to Switching History fragment when clicked
        binding.switchingHistoryButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_switchingHistoryFragment
       ))

        //Navigate to Outlet List fragment for Outstanding Product when clicked
        binding.outstandingOrderButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_outletListFragment
        ))


        // Set action bar title to "Main Dashboard"
        (activity as AppCompatActivity).supportActionBar?.title = "Main Dashboard"

        // Declare that this fragment has menu
        setHasOptionsMenu(true)

        (activity as AppCompatActivity).supportActionBar?.show()
        (activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false)


        //Return.... i don't know.
        return binding.root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater?.inflate(R.menu.nav_overflow_menu, menu)
    }
}

这是我的导航图(我将LoginFragment设置为主页)

我想在DashboardFragment中显示导航抽屉,而不是LoginFragment. (使用LoginFragment仍然是图形的开始).我已经隐藏了DashboardFragment的向上"按钮

I want to show the navigation drawer in the DashboardFragment instead of LoginFragment. (With LoginFragment still be the start of the graph). I already hide the Up button of the DashboardFragment

是仪表板的当前外观.如您所见,后退按钮已经消失.

This is the current look of the dashboard. As you can see that the back button is already gone.

我有什么可以做的吗?如果有不清楚的地方,请通知我.

Is there anything i can do with it ? If there is something unclear let me know.

推荐答案

尝试一下,希望对您有所帮助,因为它是graph上的startDestination,因此将在LoginFragment上隐藏actionBar,否则显示

try this I hope that it will help you, this will hide actionBar on LoginFragment as it's the startDestination on graph and show it otherwise

class MainActivity : AppCompatActivity() {

private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.nav_host_fragment)



    // prevent nav gesture if not on start destination
    navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
        if (nd.id == nc.graph.startDestination) {
         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
         supportActionBar?.hide()
        } else {
         supportActionBar?.show()
         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
        }
    }

     // here this ids are form fragment which should show navigation icon
    appBarConfiguration = AppBarConfiguration(setOf(
            R.id.nav_home,R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

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 onSupportNavigateUp(): Boolean {
    val navController = findNavController(R.id.nav_host_fragment)
    return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
 }
}

这篇关于Android Kotlin在某些片段中显示导航抽屉图标而不是“后退按钮"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆