我创建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. 动态添加MDL导航抽屉
- 2. 如何添加项目动态点击导航抽屉
- 3. 如何在导航抽屉中将项目添加为项目
- 4. 导航抽屉项目
- 5. 项目导航抽屉打开活动
- 6. 在导航抽屉标题中添加TextView项目的代码
- 7. 在片段上添加导航抽屉
- 8. 自定义导航抽屉及项目
- 9. 抽屉导航对于现有项目
- 10. 导航抽屉状态栏
- 11. 在Android导航抽屉中手动切换导航选项卡
- 12. 在导航抽屉中添加可展开的菜单项
- 13. 将导航抽屉活动模板添加到现有项目中
- 14. 添加按钮,导航抽屉片段
- 15. 如何在导航抽屉活动中添加Google地图?
- 16. 在导航抽屉中导航错误?
- 17. 如何在使用导航抽屉的片段时在底部导航栏上添加导航抽屉?
- 18. 具有静态和动态菜单项的导航抽屉
- 19. 如何在导航抽屉的各个项目中添加两个图标
- 20. 添加导航抽屉通过替换在导航栏中的导航栏
- 21. 反应导航抽屉和静态选项卡导航
- 22. 新的活动不启动时,点击导航抽屉项目
- 23. 添加图标在导航抽屉手动
- 24. 如何将选项卡式活动添加到导航抽屉活动中?
- 25. Android导航抽屉?
- 26. 添加列表到导航抽屉,菜单项不起作用
- 27. 无法从导航抽屉项目加载片段
- 28. 如何将Recycleview项目移动到导航抽屉的片段?
- 29. 导航抽屉与导航抽屉片段
- 30. NoSuchFieldError在抽屉式导航活动
你要加载导航抽屉itmes动态吗? –
是的。全部来自数据库。 –
可能的重复:http://stackoverflow.com/questions/31722566/dynamic-adding-item-to-navigationview-in-android –