我创建Drawer.But我想设置抽屉Itemlist是动态的.Means从数据库中获取数据并设置为drawerList。可能吗?是的比如何?我也知道静态抽屉。在导航抽屉中添加项目动态
回答
试试这个:
final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
menu.add("Runtime item "+ i);
}
谢谢...但我不想要这样。我想为它设置数据库数据。 –
是的,你可以使用这个。如果您使用的是asyncTask,则必须将此代码放在onpostExecute()上。 –
你可以给我发几个代码吗? –
您的搜索范围缩小到抽屉式导航与列表视图。如果你有listview,你可以用适配器来操作数据。
查看这个例子。 https://youtu.be/rs4LW3GxOgE
是有可能,这将是您的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/login_drawer"
>
<LinearLayout
android:id="@+id/linearlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
//create you toolbar and include in here
<include
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></include>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
<include layout="@layout/drawerlayout" />
</android.support.v4.widget.DrawerLayout>
,你抽屉布局将是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
现在你可以创建另一个布局,这将是该项目的抽屉菜单,即如果其唯一的文本,然后使用textview布局,否则,如果它是图像和文本,然后作出相应的布局
然后只是添加此视图(子XML)动态地使用layoutinflater和LinearLayout中添加视图,如:
linearlayout.addView(childview);
感谢@Dev
动态地添加的项目,我们可以得到使用使用getMenu()方法菜单对象NavigationView然后我们可以使用那个Menu对象将项目添加到导航抽屉中。
这样的:
final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 3; i++) {
menu.add("Runtime item "+ i);
}
使用子,我们可以添加一个分部和物品进入它。
// adding a section and items into it
final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
for (int i = 1; i <= 2; i++) {
subMenu.add("SubMenu Item " + i);
}
//
- 1. 添加项目到导航抽屉
- 2. 动态添加MDL导航抽屉
- 3. 如何添加项目动态点击导航抽屉
- 4. 如何在导航抽屉中将项目添加为项目
- 5. 导航抽屉项目
- 6. 添加导航抽屉AppCompatActivity
- 7. 在导航抽屉中禁用项目
- 8. 项目导航抽屉打开活动
- 9. 在导航抽屉标题中添加TextView项目的代码
- 10. 在导航抽屉中的项目旁边添加图标
- 11. 如何使用ArrayAdapter添加项目到导航抽屉活动
- 12. 在导航中添加图标抽屉
- 13. 如何在抽屉式导航图标添加到项目
- 14. 在片段上添加导航抽屉
- 15. 在导航抽屉上添加EditText Android
- 16. 如何从xml动态生成导航抽屉项目?
- 17. 抽屉导航对于现有项目
- 18. 自定义导航抽屉及项目
- 19. 可折叠导航抽屉项目
- 20. 导航抽屉状态栏
- 21. 在Android导航抽屉中手动切换导航选项卡
- 22. 在导航抽屉中添加可展开的菜单项
- 23. 将导航抽屉活动模板添加到现有项目中
- 24. 添加按钮,导航抽屉片段
- 25. 向导航抽屉添加switch语句
- 26. 将标题添加到导航抽屉
- 27. 将图标添加到导航抽屉
- 28. Android添加页脚到导航抽屉
- 29. 导航抽屉
- 30. 如何在所有活动中添加导航抽屉
你要加载导航抽屉itmes动态吗? –
是的。全部来自数据库。 –
可能的重复:http://stackoverflow.com/questions/31722566/dynamic-adding-item-to-navigationview-in-android –