`drawerlayout.addDrawerListener()` 用于什么?

huangapple go评论93阅读模式
英文:

Android Studio. For what is drawerlayout.addDrawerListener() good for?

问题

我想知道为什么需要在NavigationBar中使用以下代码片段?

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);

如果我删除drawerLayout.addDrawerListener(actionBarDrawerToggle),当我点击ToolBar中的汉堡图标时,导航栏仍然会打开。导航栏内部的点击似乎由NavigationView.setNavigationItemSelectListener()处理。

官方文档说要添加Drawerlistener():

> 将指定的侦听器添加到将收到抽屉事件通知的侦听器列表中。

我不理解这一点。抽屉事件是什么意思。如果有人能解释一下,我会非常感激。

英文:

i wonder why its needed to use for the NavigationBar the following code snippet?

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);

If i delete drawerLayout.addDrawerListener(actionBarDrawerToggle), the Navigation Bar still opens when i cklick the HamburgerIcon from my ToolBar. Clicks inside the NavBar seems to be handled by NavigationView.setNavigationItemSelectListener().

The official documentation says to addDrawerlistener():

> Adds the specified listener to the list of listeners that will be notified of drawer events.

I dont understand this. What is meant by drawer events. Would be very thankfull, if someone could explain to me.

答案1

得分: 0

addListener函数允许您指定一个在抽屉打开或关闭时调用的函数。您正在与ActionBarDrawerToggle一起使用它,它具有一个监听函数,该函数会更改关联的文本字符串。您会注意到,当您打开抽屉时,它会从“open”更改为“close”,关闭时又变回“open”。

这在某种程度上是对其的一种微不足道的用法,但也有可能需要执行其他操作的情况。例如,您可以使用它来在打开抽屉时进行网络调用以刷新抽屉中的数据。或者当抽屉打开时,您可能希望UI的其他区域变为不可见。这是一个机会,可以挂接任何您的应用可能需要的非标准功能。

英文:

The addListener function allows you to specify a function that will be called when the drawer opens or closes. You're using a ActionBarDrawerToggle with it, which has a listener function which changes the associated text string. You notice that it will change from "open" to "close" when you open the drawer, and back when you close.

This is kind of a trivial use of it, but there are situation where you may need to do other things. For example you could use it to make a network call to refresh the data in the drawer when it's opened. Or you may want other areas of your UI to go invisible when the drawer is open. It's a chance to hook in any functionality your app may need that isn't standard.

答案2

得分: 0

Sure, here are the translations:

The official documentation says to addDrawerlistener():
Adds the specified listener to the list of listeners that will be notified of drawer events.

官方文档说要添加addDrawerlistener():
将指定的监听器添加到将会收到抽屉事件通知的监听器列表中。

I dont understand this. What is meant by drawer events.

我不明白这个。什么是指抽屉事件?

If you click through to the documentation of the DrawerListener itself, the methods are fairly self-descriptive and well-documented.

如果你点击链接查看DrawerListener的文档本身,方法都相当自我描述并且有很好的文档说明。

If i delete drawerLayout.addDrawerListener(actionBarDrawerToggle), the Navigation Bar still opens when i cklick the HamburgerIcon from my ToolBar.

如果我删除drawerLayout.addDrawerListener(actionBarDrawerToggle),当我从我的工具栏单击Hamburger图标时,导航栏仍然会打开。

The listener you are setting is an ActionBarDrawerToggle which, among other things, implements the DrawerListener interface to animate the back arrow as the drawer opens and closes. You can see this for yourself by looking at the source code.

你设置的监听器是ActionBarDrawerToggle,它实现了DrawerListener接口,用于在抽屉打开和关闭时对返回箭头进行动画处理,除其他功能外。你可以通过查看源代码来自己了解这一点。

英文:

> The official documentation says to addDrawerlistener():
>> Adds the specified listener to the list of listeners that will be notified of drawer events.

> I dont understand this. What is meant by drawer events.

If you click through to the documentation of the DrawerListener itself, the methods are fairly self-descriptive and well-documented.

> If i delete drawerLayout.addDrawerListener(actionBarDrawerToggle), the Navigation Bar still opens when i cklick the HamburgerIcon from my ToolBar.

The listener your are setting is an ActionBarDrawerToggle which, among other things, implements the DrawerListener interface to animate the back arrow as the drawer opens and closes. You can see this for yourself by looking at the source code.

huangapple
  • 本文由 发表于 2023年4月19日 23:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定