2016-08-17 54 views
1

我正在开发使用Android官方NavigationDrawer一个应用程序,该navDrawer有activity_main_drawer.xml称为自己的菜单XML在下面的图片:navDrawer的自定义菜单适配器?

activity_main_drawer screenshot

这是它的应用程序:

app screenshot

现在我想要动态改变每行的标题和图标(JSON),我知道如何制作自定义ListView适配器,但我无法知道如何为此导航器制作自定义菜单适配器。这不是一个ListView,它是一个菜单,并有组和项目。

任何帮助非常感谢。

+0

可能的[如何在android中创建自定义导航抽屉]的副本(http://stackoverflow.com/questions/21796209/how-to-create-a-custom-navigation-drawer-in-android) – random

回答

1

您需要添加菜单项动态象下面这样:

Menu menu = nvDrawer.getMenu(); 
     for (MenuItem mi : menu.values()) { 
      if (menu.size() == 0) { 
       menu.add(mi.getId() + ""); 
      } 
      if (menu.getItem(mi.getId()) == null) { 
       menu.add(mi.getId() + ""); 
      } 
      MenuItem mi = menu.getItem(mi.getId()); 
      mi.setIcon(mi.getIcon()); 
      mi.setTitle(mi.getTittle()); 
     } 
+0

什么是'KSMenuItem'? –

+0

@FadiObaji well KSMenuItem是一个带菜单数据的POJO。你可以简单地使用MenuItem。 –

+0

我得到无法解决'菜单'和'价值()' 帮助 –

0

U可以添加ListView控件或回收站查看导航抽屉 只不过是你需要在你的视图中的XML标记是

android:layout_gravity="start" // This is indicates your navigation drawer 

后您的布局应如下所示(您可以用Listview替换Recyclerview) RelativeLayout rl_drawer_nav_bar将用作导航抽屉。 之后,你并不需要内置提供NavigationDrawer查看

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<LinearLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include layout="@layout/toolbar" /> 

</LinearLayout> 

<RelativeLayout 
    android:id="@+id/rl_drawer_nav_bar" 
    android:layout_width="280dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start"> 

    <RelativeLayout 
     android:id="@+id/rl_drawer_main_header_title_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp"> 

     <ImageView 
      android:id="@+id/iv_drawer_user_image" 
      android:layout_width="110dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentTop="true" 
      android:contentDescription="@string/app_name" 
      android:src="@drawable/menu_logo" /> 

     <TextView 
      android:id="@+id/tv_drawer_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" /> 


    </RelativeLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv_drawer_menu_list" 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/rl_drawer_main_header_title_bar" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" /> 

</RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 

您的API响应后收杆JSON列出YourMenuModel和

YourCustomAdapter adapter = new YourCustomAdapter(yourMenuModelList) 

每当你的菜单列表的修改或更新,或者你增加了新的菜单,只请拨打 adapter.notifyDataSetChanged()。

+0

我知道我可以做到这一点,但我特别提到,我想保留菜单项并动态改变它,它看起来更好,列表视图 –