带有标题的ExpandableListView导航抽屉

时间:2015-10-19 13:56:49

标签: java android expandablelistview expandablelistadapter

我一直在为我的应用程序工作导航抽屉,我决定使用ExpandableListView构建它。

我的问题是如何在导航栏的顶部添加标题(例如用户图像配置文件和用户的全名)?

我正在使用自定义的ExpandableListAdapter。

如果需要任何代码,请询问。我马上发布。

修改

这是导航抽屉的实现:

ExpandableListAdapter.java

origin

activity_drawer_layout.xml

package co.eshg.drawertest;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataItem; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataItem,
                                 HashMap<String, List<String>> listChildData) {
        this.context = context;
        this.listDataItem = listDataItem;
        this.listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(this.listDataItem.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 inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.drawer_child_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.tvChildItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List childList = listDataChild.get(listDataItem.get(groupPosition));
        if (childList != null && ! childList.isEmpty()) {
            return childList.size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.listDataItem.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataItem.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 inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.drawer_list_item, null);
        }

        ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);
        if ( getChildrenCount( groupPosition ) == 0 ) {
            indicator.setVisibility( View.INVISIBLE );
        } else {
            indicator.setVisibility( View.VISIBLE );
            indicator.setImageResource( isExpanded ? R.drawable.ic_keyboard_arrow_down_white_24dp
                    : R.drawable.ic_keyboard_arrow_up_white_24dp );
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tvListItem);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <FrameLayout
        android:id="@+id/activity_frame"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>


    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:groupIndicator="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:background="#444444" />

</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

编辑2:

  

要在现有导航抽屉布局中添加标题,您只需添加标题xml值并引用另一个xml布局。

以上内容不适用于您的实施。

使用NavigationViewFrameLayout的自定义ExpandableListView,请尝试此操作。

YourActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <FrameLayout hold the fragments />

    <include
        layout="@layout/your_navigation_view.xml"/>

</android.support.v4.widget.DrawerLayout>

your_navigation_view.cml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nv_navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/drawer_header" />

        <ExpandableListView
            android:id="@+id/left_drawer"
            android:layout_height="match_parent"
            android:layout_width="240dp"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:groupIndicator="@android:color/transparent"
            android:divider="@android:color/transparent"
            android:background="#444444" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

修改

将此作为自定义NavigationDrawer的参考 Reference

您必须制作所有自定义布局并手动管理它们。