【问题标题】:how to close navigation drawer on item click如何在项目单击时关闭导航抽屉
【发布时间】:2022-01-04 00:57:58
【问题描述】:

我正在尝试使用以下代码关闭项目中的导航抽屉:

   override fun onNavigationItemSelected(item: MenuItem): Boolean {
        drawerLayout.closeDrawer(GravityCompat.START)

        return true
    }

我做了一些研究,但到目前为止没有帮助,我什至尝试过this..

但是由于某种原因它没有关闭,请问可能是什么问题??

【问题讨论】:

    标签: android-studio kotlin navigation-drawer


    【解决方案1】:

    我没有添加:

    navigationView.setNavigationItemSelectedListener(this)
    

    onCreate 函数..我已经添加了它,它工作正常..

    这是它的样子;

    class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        setSupportActionBar(toolbar)
        val toggle = ActionBarDrawerToggle(this ,drawerLayout, toolbar, R.string.open, R.string.close)
        toggle.isDrawerIndicatorEnabled = true
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
    
        navigationView.setNavigationItemSelectedListener(this)
    }
    
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        drawerLayout.closeDrawer(GravityCompat.START)
    
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 2013-10-12
      • 2014-04-02
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多