问题
Hi guys, I wanna create the drawer navigation. I have read some docs, but it didnt work, so I will show u the picture that I want my drawer layout to look like this. Hope you can spend some to help me :)
回答1:
You must make an ExpandableListView in your NavigationDrawer
ExpandableListView : here
NavigationDrawer : here
Example :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Menu right -->
<LinearLayout
android:id="@+id/menu_droite"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
android:layout_gravity="end">
<!-- Liste du menu droite -->
<ExpandableListView
android:id="@+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="480dp"
android:choiceMode="singleChoice"
android:listSelector="#037DA6"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
android:groupIndicator="@drawable/group_indicator"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
ExpandableListAdapter.java :
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.NORMAL);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
回答2:
Navigation drawer docs here: https://developer.android.com/training/implementing-navigation/nav-drawer.html You should create your own layout for it and place there an ExpandableListView. List item should also have custom layout for these blue counters
P.S. Icon at the top right corner is for opening menu, not for navigation drawer. To open drawer you should touch app icon. See guidelines.
来源:https://stackoverflow.com/questions/22656081/how-to-create-drawer-navigation-like-this