从数组列表中设置抽屉项目并在 MaterialDrawer 中单击
Set drawer items from array list and clicks in MaterialDrawer
在我的 android 应用程序中,我使用 greeDAO ORM for sqlite。所以我检索了我的类别列表:
List<ArticleCategory> categories = articleCategoryDao.queryBuilder().list();
每个 ArcticleCategory
对象都有名称和描述 属性 所以我可以将它们用于抽屉的名称和描述 items.my 问题是如何将此列表添加到 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
您可以通过遍历项目来完成此操作。
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
编写逻辑。您的 "static" 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;
}
})
在我的 android 应用程序中,我使用 greeDAO ORM for sqlite。所以我检索了我的类别列表:
List<ArticleCategory> categories = articleCategoryDao.queryBuilder().list();
每个 ArcticleCategory
对象都有名称和描述 属性 所以我可以将它们用于抽屉的名称和描述 items.my 问题是如何将此列表添加到 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
您可以通过遍历项目来完成此操作。
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
编写逻辑。您的 "static" 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;
}
})