Android Studio Project
Main.java
public class Main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private DrawerLayout mDrawerLayout; ExpandableListAdapter mMenuAdapter; ExpandableListView expandableList; List<ExpandedMenuModel> listDataHeader; HashMap<ExpandedMenuModel, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);// Toolbar Action setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); expandableList = (ExpandableListView) findViewById(R.id.navigationmenu); if (navigationView != null) { setupDrawerContent(navigationView); } prepareListData(); mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList); // setting list adapter expandableList.setAdapter(mMenuAdapter); expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { //Log.d("DEBUG", "submenu item clicked"); return false; } }); expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { //Log.d("DEBUG", "heading clicked"); return false; } }); } private void prepareListData() { listDataHeader = new ArrayList<ExpandedMenuModel>(); listDataChild = new HashMap<ExpandedMenuModel, List<String>>(); ExpandedMenuModel item1 = new ExpandedMenuModel(); item1.setIconName("Home"); item1.setIconImg(android.R.drawable.ic_dialog_alert); // Adding data header listDataHeader.add(item1); ExpandedMenuModel item2 = new ExpandedMenuModel(); item2.setIconName("heading2"); item2.setIconImg(android.R.drawable.ic_delete); listDataHeader.add(item2); ExpandedMenuModel item3 = new ExpandedMenuModel(); item3.setIconName("heading3"); item3.setIconImg(android.R.drawable.ic_delete); listDataHeader.add(item3); // Adding child data List<String> heading1 = new ArrayList<String>(); heading1.add("Submenu of item 1"); List<String> heading2 = new ArrayList<String>(); 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); } private void setupDrawerContent(NavigationView navigationView) { //revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); mDrawerLayout.closeDrawers(); return true; } }); } @Override public boolean onNavigationItemSelected(MenuItem item) { return false; } }main.xml<?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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:layout_marginTop="60dp" app:headerLayout="@layout/nav_header_main" android:fitsSystemWindows="true"> <ExpandableListView android:id="@+id/navigationmenu" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="165dp" android:background="@android:color/white" android:groupIndicator="@null" /> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>app_bar_main.xml<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="co.mj.bmit.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/flContent" android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>ExpandableListAdapter.javapublic class ExpandableListAdapter extends BaseExpandableListAdapter { String Tag=" Dashboard "; String tag=" ExpandableListAdapter "; private Context mContext; private List<ExpandedMenuModel> mListDataHeader; // header titles // child data in format of header title, child title private HashMap<ExpandedMenuModel, List<String>> mListDataChild; ExpandableListView expandList; public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader, HashMap<ExpandedMenuModel, List<String>> 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(Tag,tag+"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) { /** * @XML -slidingmenu_listheader.xml */ LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.slidingmenu_listheader, null); } TextView lblListHeader = (TextView) convertView.findViewById(R.id.submenu); lblListHeader.setTypeface(null, Typeface.NORMAL); lblListHeader.setText(headerTitle.getIconName()); ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage); headerIcon.setImageResource(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.slidingmenu_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; } }slidingmenu_listheader.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="2dp" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginLeft="8dp" android:orientation="horizontal"> <ImageView android:id="@+id/iconimage" android:layout_width="50dp" android:layout_height="50dp" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingTop="10dp"/> <TextView android:id="@+id/submenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:textColor="#000000" android:text="txet" android:textSize="20sp"/> </LinearLayout> </LinearLayout>slidingmenu_list_submenu.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/submenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:padding="10dp" android:typeface="serif" android:textColor="#000000" android:textSize="18sp"/> </LinearLayout>ExpandedMenuModel .java
public class ExpandedMenuModel { String iconName = ""; int iconImg = -1; // menu icon resource id public String getIconName() { return iconName; } public void setIconName(String iconName) { this.iconName = iconName; } public int getIconImg() { return iconImg; } public void setIconImg(int iconImg) { this.iconImg = iconImg; } }
Comments
Post a Comment