0

J'ai un problème sur mon ExpandableListAdapter. J'ai utilisé une image personnalisée comme indicator, parce que je voulais le positionner à droite et non à gauche de l'élément de menu. Je dois également cacher le indicator sur certains articles.ExpandableListView modifier drawable état étendu

Mes regards étirables comme ceci:

<?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> 

Je utilisent le state_expanded de Android.

Mon DrawerLayout a cette LinearLayout en elle:

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

Je me cache le groupIndicator normal d'ajouter manuellement la coutume Drawable plus tard dans le code.

Mon ExpandableListAdapter semble être correct:

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

Le problème est que le indicator ne soit pas mis à jour. Cependant, la sortie des Log -Statements pour l'élément à l'index 1, lors de l'ouverture est:

  1. GetGroupView: Appelé!
  2. IsExpanded est: true
  3. État Index: 1

Le indicator reste le même.

Répondre

1

Après de longues recherches et essais et erreurs, je n'ai pas trouvé un moyen de le faire fonctionner avec des états. Cependant, je les images se présente comme suit:

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

    ... 
} 

Malheureusement, la solution de nettoyage avec un étirables et un état ne fonctionnait pas. J'espère que cela aidera les autres à trébucher sur cette question.