J'ai essayé d'utiliser setIndicatorBounds et setIndicatorBoundsRelative, mais l'icône ne s'affiche pas aussi bien que prévu. Je change donc mon code comme ci-dessous:
créer une disposition de groupe:
<?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="48dp"
android:background="@drawable/header_selector" >
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/ic_home" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingRight="40dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/list_item_title" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="@drawable/counter_bg"
android:textColor="@color/counter_text_color" />
<ImageView
android:id="@+id/icon_expand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:layout_toRightOf="@+id/counter"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/navigation_expand"
android:visibility="visible" />
<ImageView
android:id="@+id/icon_collapse"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:layout_toRightOf="@+id/counter"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/navigation_collapse"
android:visibility="gone" />
</RelativeLayout>
et en utilisant cette présentation dans la classe adaptateur à l'intérieur de la méthode getGroupView:
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
NavDrawerItem itemHeader = (NavDrawerItem) getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (convertView == null) {
view = (View) inflater.inflate(R.layout.drawer_header_item, null);
} else {
view = convertView;
}
ImageView icon = (ImageView) view.findViewById(R.id.icon);
if (itemHeader.isIconVisible()) {
icon.setImageResource(itemHeader.getIcon());
} else {
icon.setVisibility(View.GONE);
}
TextView textTitle = (TextView) view.findViewById(R.id.title);
textTitle.setText(" " + itemHeader.getTitle());
TextView textCount = (TextView) view.findViewById(R.id.counter);
if (itemHeader.getCounterVisibility()) {
textCount.setText(itemHeader.getCount());
} else {
textCount.setVisibility(View.GONE);
}
ImageView iconExpand = (ImageView) view.findViewById(R.id.icon_expand);
ImageView iconCollapse = (ImageView) view
.findViewById(R.id.icon_collapse);
if (isExpanded) {
iconExpand.setVisibility(View.GONE);
iconCollapse.setVisibility(View.VISIBLE);
} else {
iconExpand.setVisibility(View.VISIBLE);
iconCollapse.setVisibility(View.GONE);
}
if (getChildrenCount(groupPosition) == 0) {
iconExpand.setVisibility(View.GONE);
iconCollapse.setVisibility(View.GONE);
}
return view;
}
Avec cette méthode, vous pouvez ajuster la position de l’icône Développer / Réduire correctement. J'espère que ça aide.