gpt4 book ai didi

java - 带标题的 ExpandableListView 抽屉导航

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:33 26 4
gpt4 key购买 nike

我一直在为我的应用程序开发一个抽屉导航,并决定使用 ExpandableListView 来构建它。

我的问题是如何将标题(例如用户图像个人资料和用户的全名)添加到抽屉导航的顶部?

我正在使用自定义 ExpandableListAdapter。

如果需要任何代码,请询问。我会立即发布。

编辑

这是我的抽屉导航的实现:

ExpandableListAdapter.java

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

activity_drawer_layout.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>

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="Example Example"
android:textSize="14sp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/email" />

<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_marginLeft="16dp"
android:text="example@gmail.com"
android:textSize="14sp"
android:textStyle="normal"
android:layout_alignBottom="@+id/circleView"
android:layout_toEndOf="@+id/circleView" />

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="60dip"
android:layout_height="60dip"
android:src="@drawable/ic_face_white_48dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:id="@+id/circleView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

最佳答案

编辑2:

To add a header to your existing navigation drawer layout, you can simply add a header xml value and reference another xml layout.

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

使用带有 FrameLayoutExpandableListView 的自定义 NavigationView,试试这个。

你的Activity.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

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

关于java - 带标题的 ExpandableListView 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33216373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com