【问题标题】:Add items in Navigation Drawer Dynamically在导航抽屉中动态添加项目
【发布时间】:2018-03-08 17:49:49
【问题描述】:

我创建了抽屉。但我想动态设置抽屉的项目列表。意思是从数据库中获取数据并设置为抽屉列表。可能吗?是的,比 How?我也知道静态抽屉。

【问题讨论】:

标签: android navigation-drawer


【解决方案1】:

试试这个:

final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
   menu.add("Runtime item "+ i);
}

【讨论】:

  • 谢谢...但我不想这样。我想给它设置数据库数据。
  • 是的,您可以使用它。如果您使用的是 asyncTask,则必须将此代码放在 postExecute() 上。
  • poonam.sxope@gmail.com
  • 谢谢先生。它工作正常。如何为这个动态菜单添加 id。
  • @GanesanJ 你必须使用这个 menu.add(0// group Id, R.id.yoru_id,100 // order, "data"//title);
【解决方案2】:

是的,这可能是您的主要布局:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/login_drawer"
       >

        <LinearLayout
            android:id="@+id/linearlayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
//create you toolbar and include in here
        <include
            layout="@layout/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></include>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>

        </LinearLayout>

        <include layout="@layout/drawerlayout" />
    </android.support.v4.widget.DrawerLayout>

你的抽屉布局会是这样的:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_linear"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   </LinearLayout>

现在您可以创建另一个布局,这将是抽屉菜单的项目,即,如果它只有文本,则仅使用 textview 进行布局,否则如果是图像和文本,则相应地进行布局

然后只需使用 layoutinflater 动态添加此视图(子 xml)并在线性布局中添加视图,例如:

linearlayout.addView(childview); 

【讨论】:

    【解决方案3】:

    使用列表视图将搜索范围缩小到导航抽屉。如果您有列表视图,则可以使用适配器操作数据。

    检查这个例子。 https://youtu.be/rs4LW3GxOgE

    【讨论】:

      【解决方案4】:

      感谢@Dev

      要动态添加 Item,我们可以使用 NavigationViewgetMenu() 方法获取 Menu 对象,然后我们可以使用该 将 Items 添加到导航抽屉中>菜单对象。

      像这样:

      final Menu menu = navigationView.getMenu();
      for (int i = 1; i <= 3; i++) {
         menu.add("Runtime item "+ i);
      }
      

      使用 SubMenu,我们可以在其中添加子部分和项目。

      // adding a section and items into it
      final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
      for (int i = 1; i <= 2; i++) {
         subMenu.add("SubMenu Item " + i);
      }
      

      //

      【讨论】:

      • 但是如何将 Fragment 附加到用户按下时应该打开的那些?
      • @SimpleGuy 您可以覆盖函数onNavigationItemSelected(MenuItem) 或使用函数Toolbar#setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener),而不是检查该菜单的ID,您可以检查添加该菜单项时分配的标题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多