1

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

 List<ArticleCategory> 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();

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

4

1 回答 1

5

要添加Categories到抽屉,你首先必须创建DrawerItems你可以通过迭代你的项目来做到这一点。

ArrayList<IDrawerItem> 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;
  }
})
于 2015-12-20T16:11:55.967 回答