I implemented a navigation drawer, with navigation view. and i am adding value in navigation view through a menu.xml file.
The best solution is to have an expandable list view in navigation view.See the code below activity_navigation_view.xml
The layout navigation header is as below navigation_view_header.xml
In your navigation view activity, set the adapter for the expandable list view. NavigationViewActivity.java
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List listDataHeader;
HashMap> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
final ActionBar ab = getSupportActionBar();
/* to set the menu icon image*/
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList= (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList();
listDataChild = new HashMap>();
// Adding data header
listDataHeader.add("heading1");
listDataHeader.add("heading2");
listDataHeader.add("heading3");
// Adding child data
List heading1= new ArrayList();
heading1.add("Submenu of item 1");
List heading2= new ArrayList();
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
public void onFragmentInteraction(Boolean isDataSaved) {
}
}
The adapter for expandable list view is as follows
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List listDataHeader,HashMap> listChildData,ExpandableListView mView)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList=mView;
}
@Override
public int getGroupCount() {
int i= mListDataHeader.size();
Log.d("GROUPCOUNT",String.valueOf(i));
return this.mListDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
int childCount=0;
if(groupPosition!=2)
{
childCount=this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
return childCount;
}
@Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
Log.d("CHILD",mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition).toString());
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon= (ImageView)convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageDrawable(headerTitle.getIconImg());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_submenu.xml is as follows
listheader.xml is as follows
I have posted whole code for clarity. Hope this helps.......