Android工具栏与导航抽屉的问题

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

Problem with android toolbar using navigation drawer

问题

  1. 列出项目

正在开发一个应用程序,使用导航抽屉和导航组件,我面临两个问题:

我特别设置了每个工具栏标题应该在的位置,但每次我切换片段时,工具栏中会瞬间显示前一个片段的名称,这个名称就是片段的名称本身。所以,它会迅速从"MySpecificFragment"变成"MyFragmentName",我希望这不会发生。我已经在onCreateView或onViewCreated中设置了标题,但没有用,问题仍然存在。
我应该如何决定片段的返回按钮走向?我想创建一个标准位置,使返回按钮箭头始终在同一个位置。但它只是返回到前一个片段(这不是真正的问题,但我希望改进其行为)。
抱歉没有提供代码,我不知道应该显示什么,因为我正在与标准的Android行为相抵触。

附言:使用Android Studio和Kotlin。

英文:
  1. List the item

I'm developing an app using a navigation drawer and navigation components and I'm facing two issues:

I settled specifically each toolbar title where it is supposed to be, but every time I change the fragments, in the toolbar, for an instant, I can see the previous name from the fragment, which is the fragment name itself. So, it quickly changes from MySpecificFragment to MyFragmentName and I would like it to not happen. I've settled the title even onCreateView or onViewCreated. It didn't matter, still happening.
How could I decide the direction in which the back button of the fragment goes? I would like to create a standard position where the back button arrow goes, always the same. But it just travels back to the previous fragment (which is not a real problem, but I would like to improve its behavior)
Sorry for the lack of code, I don't know what I am supposed to display since I'm going against the standard android behavior.

P.S.: Using android studio and kotlin

答案1

得分: 0

关于第一个问题,避免在工具栏中短暂显示前一个片段名称的一种方法是在父活动中设置工具栏标题,然后从片段的 onResume() 方法中更新它。这可以确保在片段被推送到返回堆栈后重新恢复时正确设置工具栏标题。以下是一个示例代码片段:

在您的活动中:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    }

    fun setToolbarTitle(title: String) {
        supportActionBar?.title = title
    }
}

在您的片段中:

class MySpecificFragment : Fragment() {
    override fun onResume() {
        super.onResume()
        (activity as? MainActivity)?.setToolbarTitle("MySpecificFragment")
    }
}

关于第二个问题,您可以通过使用自定义的 NavController.OnDestinationChangedListener 来自定义返回按钮的行为。在监听器中,您可以根据当前和先前的目标来设置返回按钮图标和其行为。以下是一个示例代码片段:

class MyNavigationController(activity: AppCompatActivity, navController: NavController) {
    init {
        navController.addOnDestinationChangedListener(
            activity, object : NavController.OnDestinationChangedListener {
                override fun onDestinationChanged(
                    controller: NavController,
                    destination: NavDestination,
                    arguments: Bundle?
                ) {
                    if (destination.id == R.id.my_fragment) {
                        activity.supportActionBar?.setDisplayHomeAsUpEnabled(false)
                    } else {
                        activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
                        activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
                        activity.supportActionBar?.setHomeActionContentDescription(R.string.back)
                        activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
                        activity.supportActionBar?.setDisplayShowHomeEnabled(true)
                    }
                }
            })
    }
}

在这里,您可以通过设置 setDisplayHomeAsUpEnabledsetHomeAsUpIndicatorsetHomeActionContentDescription 来根据当前和先前的目标来调整返回按钮图标和行为。

英文:

Regarding the first issue, one way to avoid the brief display of the previous fragment name in the toolbar is to set the toolbar title in the parent activity and then update it from the fragment's onResume() method. This ensures that the toolbar title is set correctly when the fragment is resumed after being pushed onto the back stack. Here's an example code snippet:

In your activity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    }

    fun setToolbarTitle(title: String) {
        supportActionBar?.title = title
    }
}

In your fragment:

class MySpecificFragment : Fragment() {
    override fun onResume() {
        super.onResume()
        (activity as? MainActivity)?.setToolbarTitle("MySpecificFragment")
    }
}

Regarding the second issue, you can customize the back button behavior by using a custom NavController.OnDestinationChangedListener. In the listener, you can set the back button icon and its behavior based on the current and previous destinations. Here's an example code snippet:

class MyNavigationController(activity: AppCompatActivity, navController: NavController) {
    init {
        navController.addOnDestinationChangedListener(
            activity, object : NavController.OnDestinationChangedListener {
                override fun onDestinationChanged(
                    controller: NavController,
                    destination: NavDestination,
                    arguments: Bundle?
                ) {
                    if (destination.id == R.id.my_fragment) {
                        activity.supportActionBar?.setDisplayHomeAsUpEnabled(false)
                    } else {
                        activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
                        activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
                        activity.supportActionBar?.setHomeActionContentDescription(R.string.back)
                        activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back)
                        activity.supportActionBar?.setDisplayShowHomeEnabled(true)
                    }
                }
            })
    }
}

Here, you can adjust the back button icon and behavior based on the current and previous destinations by setting setDisplayHomeAsUpEnabled, setHomeAsUpIndicator, and setHomeActionContentDescription.

答案2

得分: 0

对于这两个问题,我已找到最佳解决方案。
第一个问题,包含快速闪现显示片段名称的问题,是由导航图文件中的标签引起的。当它被移除时,不希望的行为就停止了。事实就是如此,但要花费很长时间才能发现原因,因为只有在使用导航抽屉时才会发生这种情况。
至于第二个问题,我尝试指定按钮,但这不是一个好的选项。我无法以正确的方式清除片段堆栈,所以我的选择不是编辑按钮功能,而是有时返回到片段,我选择了popstackback,这是我之前不知道的。它帮助了很多,以避免任何意外的行为。
希望对任何人有所帮助。

英文:

For both problems I've discovered the best approach solution.
The first, the problem containing the quick flash displaying the fragment name, it was caused by the label in the nav_graph file. When it's removed, the unwanted behavior stops. It was simple as that but took a long time to discover the cause, since it only happened to me using navigation drawer.
For the second, I tried to specify the button but it was not a good option. I was not able to clean the stack of fragments in the correct manner so my option was not editing the button functionalities, but instead of sometimes navigating back to the fragment, I choose popstackback, which I didn't knew yet. It helped a lot to avoid any unexpected behavior.
Hope it helps anyone.

huangapple
  • 本文由 发表于 2023年2月18日 11:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490998.html
匿名

发表评论

匿名网友

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

确定