【问题标题】:Single activity: fragment with AppBarLayout单个活动:带有 AppBarLayout 的片段
【发布时间】:2021-11-11 17:22:20
【问题描述】:

我有一个带有RecyclerView 的片段。单击一个项目时,我正在导航(使用导航组件)到一个详细信息片段。

在细节片段中,我想实现AppBarLayout。但我已经有一个活动级别的工具栏。

我怎样才能实现这样的目标?您可以在Google design guidelines 上找到视频。不确定他们是否正在开始一项新活动。底部导航也是我的活动的一部分,在使用 AppBarLayout 导航到详细信息片段时应该可见。

【问题讨论】:

    标签: android android-fragments android-fragmentactivity android-architecture-navigation android-appbarlayout


    【解决方案1】:

    在细节片段中,我想实现 AppBarLayout。但我已经有一个活动级别的工具栏。

    所以,现在活动有一个工具栏,你想在DetailFragment 中设置另一个工具栏。并且当您使用导航组件时,您可能会将工具栏由navController 管理

    好吧,这需要您将活动级别工具栏移动到主片段中;由navHostFragment 主持。

    原因:因为从片段级别设置另一个工具栏会复制它,因为活动级别的工具栏始终存在。 Check this answer

    因此您需要在片段中设置工具栏;通常当您通过导航组件从一个片段移动到另一个片段时,不会发生重复,现在您可以根据需要使用不同的工具栏。

    这是一个示意图演示:

    活动:

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }
    
        fun setupActionBar(toolbar: Toolbar) {
            setSupportActionBar(toolbar)
            val navHostFragment =
                supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
            val navController = navHostFragment.navController
            val appBarConfiguration = AppBarConfiguration.Builder(
                R.id.fragment, R.id.fragment_b
            ) .build()
    
            NavigationUI.setupActionBarWithNavController(
                this,
                mNavController,
                navController
            )
        }
    
    }
    

    片段A

    class FragmentA : Fragment() {
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_a, container, false)
    
            (requireActivity() as MainActivity).setupActionBar(view.findViewById(R.id.fragment_toolbar))
    
    
            return view
        }
    
    }
    

    FragmentB:类似于FragmentA,但有自己的toolBar

    class FragmentB : Fragment() {
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = inflater.inflate(R.layout.fragment_b, container, false)
    
            (requireActivity() as MainActivity).setupActionBar(view.findViewById(R.id.fragment_toolbar))
    
    
            return view
        }
    
    }
    

    【讨论】:

    • 这是一个解决方案,但在我看来不是一个好的解决方案。每次导航到不同的片段时,您都必须设置工具栏。即使我在整个应用程序中只需要一个不同的工具栏。
    • @Erkan 在navGraph 中还有另一个选项可以为详细信息片段创建另一个活动,您可以为该活动setSupportActionBarnavigation components can support multiple activities
    • 谢谢,我试试。这与单一活动方法相反,但目前这对我来说是最好的解决方案。安也找到了另一种解决方案。在活动级别设置 AppBarLayout 并启用或禁用 AppBarLayout 的滚动行为。
    猜你喜欢
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多