0

Bon, alors j'ai regardé loin, j'ai pu ajouter le même élément enfant à chaque groupe, mais je suis incapable de l'obtenir pour répondre aux clics. En ce moment, il agit comme un en-tête d'enfant, et j'en ai besoin pour agir comme les autres enfants.Ajouter le même enfant cliquable au sommet de chaque groupe expandablelistview - android

Actuellement, mes listes sont remplies à partir d'une HashMap située dans un autre fichier. Parce que vous ne pouvez pas ajouter au milieu d'un HashMap sans le reconstruire complètement, j'ai pensé que la meilleure option était d'ajouter l'enfant quelque part quand la liste est accédée dans l'adaptateur. Je ne suis pas sûr où et comment.

Comme vous pouvez le voir dans getChildView(), j'ajoute ma disposition enfant répétée (expandedListItemChildHeader), si la position de child est 0. J'ai également ajouté 1 valeur à la taille de getChildrenCount().

Voici mon code adaptateur:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter { 

private Context context; 
private List<String> expandableListTitle; 
private HashMap<String, List<String>> expandableListDetail; 

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, 
            HashMap<String, List<String>> expandableListDetail) { 
    this.context = context; 
    this.expandableListTitle = expandableListTitle; 
    this.expandableListDetail = expandableListDetail; 
} 

@Override 
public Object getChild(int listPosition, int expandedListPosition) { 

    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .get(expandedListPosition); 
} 


@Override 
public long getChildId(int listPosition, int expandedListPosition) { 

    return expandedListPosition; 
} 

@Override 
public View getChildView(int listPosition, final int expandedListPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //if we're in the first position then add the list_item_header 
    if(expandedListPosition == 0) 
    { 
     convertView = layoutInflater.inflate(R.layout.list_item_header, null); 
     TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItemChildHeader); 
     expandedListTextView.setText("All Devices"); 

    } 

    //otherwise add the list_item 
    if (expandedListPosition > 0 && expandedListPosition < getChildrenCount(listPosition)) { 
     final String expandedListText = (String) getChild(listPosition, expandedListPosition - 1); 
     convertView = layoutInflater.inflate(R.layout.list_item, null); 
     TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem); 
     expandedListTextView.setText(expandedListText); 
    } 

    return convertView; 
} 

@Override 
public int getChildrenCount(int listPosition) { 
    //add a value of one to the size of the count of children per group to make 
    //room for the child header 

    return (this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .size() + 1); 
} 

@Override 
public Object getGroup(int listPosition) { 
    return this.expandableListTitle.get(listPosition); 
} 

@Override 
public int getGroupCount() { 
    return this.expandableListTitle.size(); 
} 

@Override 
public long getGroupId(int listPosition) { 
    return listPosition; 
} 

@Override 
public View getGroupView(int listPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String listTitle = (String) getGroup(listPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_group, null); 
    } 
    TextView listTitleTextView = (TextView) convertView 
      .findViewById(R.id.listTitle); 
    listTitleTextView.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL)); 
    listTitleTextView.setText(listTitle); 

    //Use this conditional to change direction of group indicator icons 
    //the ImageView in the xml layout is invisible by default. 
    ImageView groupIndicatorView = (ImageView) convertView.findViewById(R.id.expand_GroupIndicator); 
    if (getChildrenCount(listPosition) == 0) { 
     groupIndicatorView.setVisibility(View.INVISIBLE); 
    } 
    else { 
     groupIndicatorView.setVisibility(View.VISIBLE); 
     groupIndicatorView.setImageResource(isExpanded ? R.drawable.ic_expand_less_black_24dp : R.drawable.ic_expand_more_black_24dp); 
    } 

    return convertView; 
} 

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

@Override 
public boolean isChildSelectable(int listPosition, int expandedListPosition) { 
    return true; 
} 

Après mon code de liste extensible dans mon MainActivity qui est situé dans la méthode onCreate(): (note: les variables énumérées dans la partie supérieure du code exemple ci-dessous est en dehors de la méthode onCreate(), je l'ai juste inclus pour référence.

ExpandableListView expandableListView; 
ExpandableListAdapter expandableListAdapter; 
List<String> expandableListTitle; 
HashMap<String, List<String>> expandableListDetail; 


    expandableListView = (ExpandableListView) findViewById(R.id.navDrawer_userListView); 
    expandableListDetail = ExpandableListDataPump.getData(); 
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
    expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail); 
    expandableListView.setAdapter(expandableListAdapter); 

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
     //initialize int var 
     int previousGroup = 0; 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      //this conditional enables only one drop down group to open at a time 
      if(groupPosition != previousGroup) 
       expandableListView.collapseGroup(previousGroup); 
      previousGroup = groupPosition; 

      Toast.makeText(
        getApplicationContext(), 
        expandableListTitle.get(groupPosition) 
          + " List Expanded.", 
        Toast.LENGTH_SHORT 
      ).show(); 
     } 
    }); 

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

     @Override 
     public void onGroupCollapse(int groupPosition) { 

      Toast.makeText(getApplicationContext(), 
        expandableListTitle.get(groupPosition) + " List Collapsed.", 
        Toast.LENGTH_SHORT 
      ).show(); 

     } 
    }); 

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 

      v.setSelected(true); 

      //Change title on toolbar to match group selection 
      getSupportActionBar().setTitle(expandableListTitle.get(groupPosition) + "'s Devices"); 
      //Change subtitle on toolbar to match item selection 
      //must offset child position by 1 to have room for child header 
      getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1)); 
      Toast.makeText(
        getApplicationContext(), 
        expandableListTitle.get(groupPosition) 
          + " -> " 
          + expandableListDetail.get(
          expandableListTitle.get(groupPosition)).get(
          childPosition - 1), Toast.LENGTH_SHORT 
      ).show(); 

      return false; 
     } 
    }); 

Voici mes mises en page commençant par la list_item_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="7dp"> 

     <Switch 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@null" 
      android:id="@+id/toggle_allDevices" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:paddingLeft="2dp" 
      android:tint="@color/drawerContent"/> 

     <TextView 
      android:id="@+id/expandedListItemChildHeader" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@drawable/expandable_text_selector" 
      android:paddingLeft="65dp" 
      android:paddingTop="16dp" 
      android:textSize="16dp" 
      android:paddingBottom="16dp" /> 

    </RelativeLayout> 

</LinearLayout> 

Voici ma list_item.xml mise en page

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="7dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@null" 
      android:id="@+id/deviceTypeIcon" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:paddingLeft="14dp" 
      android:tint="@color/drawerContent"/> 

     <TextView 
      android:id="@+id/expandedListItem" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@drawable/expandable_text_selector" 
      android:paddingLeft="65dp" 
      android:paddingTop="16dp" 
      android:textSize="16dp" 
      android:paddingBottom="16dp" /> 

    </RelativeLayout> 

</LinearLayout> 

La mise en page de list_group.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0.25dp" 
     android:layout_alignParentTop="true" 
     android:background="@color/drawerContent" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="7dp"> 

     <TextView 
      android:id="@+id/listTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="10dp" 
      android:textColor="@color/drawerContent" 
      android:background="@color/drawerBg" 
      android:paddingTop="16dp" 
      android:textSize="16dp" 
      android:paddingBottom="16dp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/expand_GroupIndicator" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_expand_more_black_24dp" 
      android:paddingRight="15dp" 
      android:tint="@color/drawerContent" 
      android:visibility="invisible"/> 


    </RelativeLayout> 

</LinearLayout> 

Et enfin l'extrait de mon NavigationView de l'activité principale:

<android.support.design.widget.NavigationView 
     android:background="@color/drawerBg" 
     app:itemTextColor="@color/drawerContent" 
     app:itemIconTint="@color/drawerContent" 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true"> 

     <ExpandableListView 
      android:id="@+id/navDrawer_userListView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="@color/drawerContent" 
      android:textColor="@color/drawerContent" 
      android:background="@color/drawerBg" 
      android:dividerHeight="@null" 
      android:paddingTop="?attr/actionBarSize" 
      android:layoutDirection="rtl" 
      android:choiceMode="singleChoice" 
      android:groupIndicator="@null"> 
     </ExpandableListView> 

    </android.support.design.widget.NavigationView> 

Toute aide serait grandement appréciée! Merci!

+0

EDIT: D'accord, je suis heureux d'annoncer que je fixai le problème cliquable. J'ai trouvé un message obscur qui mentionnait si vous avez un widget cliquable comme un bouton ou un commutateur/bascule, cela empêche l'événement click de fonctionner. La solution simple est de mettre android: focusable = "false" dans mon switch. La seule chose que j'ai encore besoin de comprendre maintenant c'est d'amener le nouvel enfant à travailler avec le hashmap en termes de correspondance de l'index/position avec l'élément correct. –

Répondre

0

D'accord, j'ai été capable de résoudre la dernière partie de mon problème. il se trouve que tout ce que je devais faire est de mettre un conditionnel dans ma méthode onChildClick() pour ajuster l'index j'accédait en utilisant le code suivant:

//this conditional corrects for our new child header that is added in getChildView() 
      String newChildSelect = ""; 

      if (childPosition == 0) { 
       newChildSelect = "All Devices"; 
       getSupportActionBar().setSubtitle(newChildSelect); 
      } 
      else 
      { 
       newChildSelect = expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition - 1); 
       //Change subtitle on toolbar to match item selection 
       getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1)); 
      }