如何创建这样的抽屉导航?

时间:2014-03-26 09:09:40

标签: android navigation-drawer slidingdrawer drawerlayout

I want to create the drawer exactly look like this

大家好,我想制作抽屉导航。我已经阅读了一些文档,但它没有用,所以我会向你展示我希望我的抽屉布局看起来像这样的图片。希望你能花一些钱来帮助我:)。

2 个答案:

答案 0 :(得分:3)

您必须在NavigationDrawer中创建一个ExpandableListView

ExpandableListView:here

NavigationDrawer:here

示例:

    <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;
    }
}

答案 1 :(得分:1)

导航抽屉文档:https://developer.android.com/training/implementing-navigation/nav-drawer.html 您应该为它创建自己的布局并放置一个ExpandableListView。 列表项还应具有这些蓝色计数器的自定义布局

P.S。右上角的图标用于打开菜单,而不用于导航抽屉。要打开抽屉,您应触摸应用程序图标。见准则。