【问题标题】:How to create drawer navigation like this?如何创建这样的抽屉导航?
【发布时间】:2014-03-26 09:09:39
【问题描述】:

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

【问题讨论】:

标签: android navigation-drawer slidingdrawer drawerlayout


【解决方案1】:

您必须在 NavigationDrawer 中创建一个 ExpandableListView

ExpandableListView : here

导航抽屉: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;
    }
}

【讨论】:

    【解决方案2】:

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

    附:右上角的图标用于打开菜单,而不是用于导航抽屉。要打开抽屉,您应该触摸应用程序图标。请参阅指南。

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多