简体   繁体   English

如何实现一个android侧边栏菜单?

[英]how implement an android sidebar menu?

what is best solution to implement a sidebar menu like below ?实现如下侧边栏菜单的最佳解决方案是什么? items of sidebar and the content of each section comes from server(count of items is variable)侧边栏项目和每个部分的内容来自服务器(项目数是可变的)

  • section list (in left side)is scrollable部分列表(在左侧)是可滚动的
  • section item count can be more than 20 item部分项目数可以超过 20 项
  • i use MVVM我使用 MVVM


i want some advices about view implementation for best performance in implementation我想要一些关于视图实现的建议,以获得最佳的实现性能

在此处输入图片说明

You can get help with complete code from Material Design.您可以从 Material Design 获得有关完整代码的帮助。 And click here to visit the site并点击这里访问该网站

For section items at Right, you can use RecyclerView with GridLayoutManager.对于右侧的部分项目,您可以将 RecyclerView 与 GridLayoutManager 一起使用。

For the section list (on the left side) There are two possibilities based on the UI requirements.对于部分列表(在左侧),根据 UI 要求有两种可能性。

  1. You can use Navigation view for left navigation menu but not appropriate if data is being loaded from the internet您可以将导航视图用于左侧导航菜单,但不适用于从 Internet 加载数据的情况
  2. You can use a RecyclerView with LinearLayoutManager您可以将 RecyclerView 与 LinearLayoutManager 一起使用

On the other hand, if you'll use view pager that will not be good for performance since there can be even more then 10 lists.另一方面,如果您将使用视图寻呼机,这对性能不利,因为可能有超过 10 个列表。

So you can display both lists in a Fragment and load the data in the repository.因此,您可以在 Fragment 中显示两个列表并在存储库中加载数据。 And on response send data to fragment using Live data and ViewModel.并在响应时使用实时数据和 ViewModel 将数据发送到片段。 update the RecyclerViews in live data observer.在实时数据观察器中更新 RecyclerViews。 Update Contents based on selected section根据所选部分更新内容

Read about recyclerviewRead about Navigation menu 阅读关于 recyclerview阅读关于导航菜单

In this situation i found the best solution to use recycler-view for left-hand sidebar menu(because this is easy to use and handle)在这种情况下,我找到了将回收器视图用于左侧边栏菜单的最佳解决方案(因为这易于使用和处理)
and i used single fragment to showing content for each sidebar-menu item and caching content for later use(click action by user)我使用单个片段来显示每个侧边栏菜单项的内容并缓存内容供以后使用(用户单击操作)
I prefer do not use the navigation menu for three reason :我更喜欢不使用导航菜单的三个原因:

  1. I need the fix menu with no sliding action (of course i know i can fix navigation menu with no sliding)我需要没有滑动操作的修复菜单(当然我知道我可以在没有滑动的情况下修复导航菜单)
  2. more important : I fill the sidebar menu items with data that comes from server and this easy to handle for recycler-view更重要的是:我用来自服务器的数据填充侧边栏菜单项,这对于回收者视图来说很容易处理
  3. It is easier to change the sidebar-menu view based on the data and user action with recycler-view使用 recycler-view 可以更轻松地根据数据和用户操作更改侧边栏菜单视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM