0

Ich habe ein Problem auf meinem ExpandableListAdapter. Ich benutzte ein benutzerdefiniertes Bild als indicator, weil ich es nach rechts und nicht links von dem Menüpunkt positionieren wollte. Ich muss auch die indicator auf einige Elemente verstecken.ExpandableListView change drawable state_expanded

Meine ziehbar sieht wie folgt aus:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_expanded="true" 
    android:drawable="@drawable/arrow_up" /> 
<item 
    android:drawable="@drawable/arrow_down" /> 
</selector> 

Also mache ich Gebrauch des state_expanded von Android.

Mein DrawerLayout hat diese LinearLayout drin:

<LinearLayout 
    android:id="@+id/drawer_linear_layout" 
    android:layout_width="@dimen/menu_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:orientation="vertical"> 

    <ExpandableListView 
     android:id="@+id/navigationmenu" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:layout_marginTop="@dimen/nav_header_height" 
     android:childDivider="@android:color/transparent" 
     android:headerDividersEnabled="true" 
     android:groupIndicator="@android:color/transparent" 
     android:background="@drawable/border_shadow"> 
    </ExpandableListView> 

</LinearLayout> 

ich den normalen groupIndicator verstecken manuell die benutzerdefinierten Drawable später im Code hinzuzufügen.

Mein ExpandableListAdapter scheint korrekt zu sein:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    ... 

    private static final int[] EMPTY_STATE_SET = {}; 
    private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded }; 
    private static final int[][] GROUP_STATE_SETS = { 
     EMPTY_STATE_SET, // 0 
     GROUP_EXPANDED_STATE_SET //1 
    }; 

    ... 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     MOMenuItem headerTitle = (MOMenuItem) getGroup(groupPosition); 

     LayoutInflater infalInflater = (LayoutInflater) this.mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     LinearLayout linLayout = (LinearLayout) infalInflater.inflate(R.layout.list_header, parent, false); 
     convertView = linLayout; 

     View indicator = convertView.findViewById(R.id.indicator_image); 

     if (groupPosition == 1) { 
      Log.d("GetGroupView", "Called!"); 
     } 

     if (indicator != null) { 
      ImageView indicatorImage = (ImageView) indicator; 
      if (getChildrenCount(groupPosition) == 0) { 
       indicatorImage.setVisibility(View.INVISIBLE); 
      } else { 
       if (groupPosition == 1) { 
        Log.d("IsExpanded is", "" + isExpanded); 
       } 
       indicatorImage.setVisibility(View.VISIBLE); 
       int stateSetIndex = (isExpanded ? 1 : 0); 
       if (groupPosition == 1) { 
        Log.d("State Index", "" + stateSetIndex); 
       } 
       Drawable drawable = indicatorImage.getDrawable(); 
       drawable.setState(GROUP_STATE_SETS[stateSetIndex]); 
      } 
     } 

     ... 

     return convertView; 
    } 
} 

Das Problem ist, dass die indicator nicht aktualisiert. Allerdings ist die Ausgabe der Log -Aussagen für das Element mit dem Index 1, wenn sie geöffnet ist:

  1. GetGroupView: Genannt!
  2. IsExpanded ist: true
  3. State Index: 1

Die indicator gleich bleibt.

Antwort

1

Nach langer Forschung und Versuch und Irrtum fand ich keinen Weg, um es mit Staaten arbeiten zu lassen. Allerdings habe ich die Bilder selbst wie folgt aus:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    MOMenuItem headerTitle = (MOMenuItem) getGroup(groupPosition); 

    LayoutInflater infalInflater = (LayoutInflater) this.mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout linLayout = (LinearLayout) infalInflater.inflate(R.layout.list_header, parent, false); 
    convertView = linLayout; 

    View indicator = convertView.findViewById(R.id.indicator_image); 

    if (indicator != null) { 
     ImageView indicatorImage = (ImageView) indicator; 
     if (getChildrenCount(groupPosition) == 0) { 
      indicatorImage.setVisibility(View.INVISIBLE); 
     } else { 
      indicatorImage.setVisibility(View.VISIBLE); 

      if (isExpanded) { 
       indicatorImage.setImageResource(R.drawable.arrow_up); 
      } else { 
       indicatorImage.setImageResource(R.drawable.arrow_down); 
      } 
     } 
    } 

    ... 
} 

Leider die Reinigungslösung mit einem ziehbar und einem Zustand, funktionierte nicht. Ich hoffe, dies wird anderen helfen, über diese Frage zu stolpern.

Verwandte Themen