在我的Android应用程序中,我将greeDAO ORM用于sqlite。所以我检索了我的类别列表:
Listcategories = 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();
请记住,我有一些手动手动生成的静态项目以及一些来自数据库的动态项目,因此在这种情况下,单击事件对我来说非常重要。
要将其添加Categories
到抽屉中,首先必须创建,DrawerItems
您可以通过遍历商品来做到这一点。
ArrayListdrawerItems = 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; } })