当前位置:  开发笔记 > 编程语言 > 正文

从数组列表中设置抽屉项目,然后在MaterialDrawer中单击

如何解决《从数组列表中设置抽屉项目,然后在MaterialDrawer中单击》经验,为你挑选了1个好方法。

在我的Android应用程序中,我将greeDAO ORM用于sqlite。所以我检索了我的类别列表:

 List categories = articleCategoryDao.queryBuilder().list(); 

每个ArcticleCategory对象都有名称和描述属性,因此我可以将其用于抽屉项目的名称和描述。我的问题是如何将此列表添加到myDrawer项目以及如何管理其单击事件。这是我的抽屉代码:

    Drawer myDrawer = new DrawerBuilder().withActivity(this).withToolbar(toolbar)
            .addDrawerItems(
                    new PrimaryDrawerItem().withName("Home").withIcon(GoogleMaterial.Icon.gmd_import_contacts).withIconColor(Color.BLACK)
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                    return false;
                }
            })
            .withAccountHeader(navigationHeader)
            .withDrawerGravity(GravityCompat.START)
            .build();

请记住,我有一些手动手动生成的静态项目以及一些来自数据库的动态项目,因此在这种情况下,单击事件对我来说非常重要。



1> mikepenz..:

要将其添加Categories到抽屉中,首先必须创建,DrawerItems您可以通过遍历商品来做到这一点。

ArrayList drawerItems = new ArrayList<>();
for(ArticleCategory category : categories) {
   drawerItems.add(new PrimaryDrawerItem().withName(category.getName()).withDescription(category.getDescription()));
   //if you have a id you can also do: .withIdentifier(category.getIdentifier());
   //depending on what you need to identify or to do the logic on click on one of those items you can also set a tag on the item: .withTag(category);
}

创建项目后,将其添加到 DrawerBuilder

drawerBuilder.withDrawerItems(drawerItems);

现在,在创建抽屉时,您必须为编写逻辑Listener。您的“静态” DrawerItem应该定义一个标识符,这样,如果单击其中之一,则可以直接做出反应

.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
  @Override
  public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
      if (drawerItem != null) {
          Intent intent = null;
          if (drawerItem.getIdentifier() == 1) {
              //static item with ID 1
          } else if (drawerItem.getIdentifier() == 2) {
              //static item with ID 2
          } else {
              //if none of your static items were clicked handle the logic for the categories. 
              //now you have the drawerItem which were created from a category
              //you can identify them by identifier, their tag, or name. Depends on what you need to do your logic here

          }
      }

      return false;
  }
})

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有