2016-12-03 68 views
-2

我创建Drawer.But我想设置抽屉Itemlist是动态的.Means从数据库中获取数据并设置为drawerList。可能吗?是的比如何?我也知道静态抽屉。在导航抽屉中添加项目动态

+0

你要加载导航抽屉itmes动态吗? –

+0

是的。全部来自数据库。 –

+0

可能的重复:http://stackoverflow.com/questions/31722566/dynamic-adding-item-to-navigationview-in-android –

回答

3

试试这个:

final Menu menu = navigationView.getMenu(); 
for (int i = 1; i <= 10; i++) { 
    menu.add("Runtime item "+ i); 
} 
+0

谢谢...但我不想要这样。我想为它设置数据库数据。 –

+1

是的,你可以使用这个。如果您使用的是asyncTask,则必须将此代码放在onpostExecute()上。 –

+0

你可以给我发几个代码吗? –

0

您的搜索范围缩小到抽屉式导航与列表视图。如果你有listview,你可以用适配器来操作数据。

查看这个例子。 https://youtu.be/rs4LW3GxOgE

0

是有可能,这将是您的主要布局:

<?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); 
0

感谢@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); 
} 

//