gpt4 book ai didi

android - 从数组列表中设置抽屉项目并在 MaterialDrawer 中单击

转载 作者:行者123 更新时间:2023-11-29 15:08:30 33 4
gpt4 key购买 nike

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

 List<ArticleCategory> categories = articleCategoryDao.queryBuilder().list(); 

每个 ArcticleCategory 对象都有 name 和 description 属性,所以我可以将它们用于抽屉项目的名称和描述。我的问题是如何将此列表添加到 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 编写逻辑。您的“静态”DrawerItem 应该定义一个标识符,这样当其中一个被点击时您可以直接使用react

.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 - 从数组列表中设置抽屉项目并在 MaterialDrawer 中单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379282/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com