2012-03-22 14 views
45

Sto provando a programmare un'interfaccia Android che utilizza un elenco espandibile sul lato e un frammento sull'altro lato in modo da poter caricare risorse diverse facendo clic sui figli dell'elenco espandibile. Ma sfortunatamente non riesco a trovare nessun buon tutorial su questa lista. Sì, ho dato un'occhiata alle demo dell'API e ho creato una lista normale con BaseExpandableListAdapter, ma ancora, comprendendo bene quell'elenco, è piuttosto difficile senza un buon tutorial, ne hai uno in giro o informazioni che potrei controllare?Android ExpandableListView - Alla ricerca di un tutorial

+0

Non posso credere che questo è stato contrassegnato come "non costruttivo"! E 'stato molto costruttivo e le risposte molto utili! Grazie per aver postato questa domanda, DeX03. –

risposta

50

si possono trovare ad esempio di lavoro della vista elenco espandibile dai seguenti link:

per fare clic su figlio, è possibile gestire in questo modo.

getExpandableListView().setOnChildClickListener(new OnChildClickListener() {     
    public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 
     // your code... 
    } 
}); 

Spero che questo ti possa aiutare. Grazie ..

+0

ma ciò non funziona allo stesso modo per attività che non estendono ExpandableListActivity perché è quello che ho, come faccio a funzionare in quello? – DeX03

+0

non ho ancora risolto il problema, un grande aiuto! – DeX03

58

Crea elenco di elementi

List<ParentItem> itemList = new ArrayList<ParentItem>(); 

ParentItem parent1 = new ParentItem(); 
parent1.getChildItemList().add(new ChildItem()); 
parent1.getChildItemList().add(new ChildItem()); 
parent1.getChildItemList().add(new ChildItem()); 

ParentItem parent2 = new ParentItem(); 
parent2.getChildItemList().add(new ChildItem()); 
parent2.getChildItemList().add(new ChildItem()); 
parent2.getChildItemList().add(new ChildItem()); 

itemList.add(parent1); 
itemList.add(parent2); 

ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(context, itemList); 

Data Objects

public class ParentItem { 

     private List<ChildItem> childItemList; 

     public ParentItem() { 
       childItemList = new ArrayList<ChildItem>(); 
     } 

     public List<ChildItem> getChildItemList() { 
       return childItemList; 
     } 
} 

public class ChildItem { 
     // filll with your data 
} 

adattatore

public class ExpandableListViewAdapter extends BaseExpandableListAdapter { 

     private static final class ViewHolder { 
       TextView textLabel; 
     } 

     private final List<ParentItem> itemList; 
     private final LayoutInflater inflater; 

     public ExpandableListViewAdapter(Context context, List<ParentItem> itemList) { 
       this.inflater = LayoutInflater.from(context); 
       this.itemList = itemList; 
     } 

     @Override 
     public ChildItem getChild(int groupPosition, int childPosition) { 

       return itemList.get(groupPosition).getChildItemList().get(childPosition); 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
       return childPosition; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
       return itemList.get(groupPosition).getChildItemList().size(); 
     } 

     @Override 
     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 
           final ViewGroup parent) { 
       View resultView = convertView; 
       ViewHolder holder; 


       if (resultView == null) { 

         resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id 
         holder = new ViewHolder(); 
         holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id 
         resultView.setTag(holder); 
       } else { 
         holder = (ViewHolder) resultView.getTag(); 
       } 

       final ChildItem item = getChild(groupPosition, childPosition); 

       holder.textLabel.setText(item.toString()); 

       return resultView; 
     } 

     @Override 
     public ParentItem getGroup(int groupPosition) { 
       return itemList.get(groupPosition); 
     } 

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

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

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, View theConvertView, ViewGroup parent) { 
       View resultView = theConvertView; 
       ViewHolder holder; 

       if (resultView == null) { 
         resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id 
         holder = new ViewHolder(); 
         holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id 
         resultView.setTag(holder); 
       } else { 
         holder = (ViewHolder) resultView.getTag(); 
       } 

       final ParentItem item = getGroup(groupPosition); 

       holder.textLabel.setText(item.toString()); 

       return resultView; 
     } 

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

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
       return true; 
     } 

} 

Ti dà

============================== 
+Parent 1 
============================== 
-child 1.1 
============================== 
-child 1.2 
============================== 
-child 1.3 
============================== 
+Parent 2 
============================== 
-child 2.1 
============================== 
-child 2.2 
============================== 
-child 2.3 
============================== 
+1

ma, come posso gestire l'evento click sui figli degli elenchi? perché questo è il problema principale con tutte le esercitazioni che ho trovato – DeX03

+0

'Semi due punti (;)' mancante ' itemList = new ArrayList ()' –

+1

Sto diventando nullo 'holder.textLabel.setText (item.toString()); ', Come risolto? –

1

questo modo è possibile gestire gli eventi:

getExpandableListView().setOnChildClickListener(new OnChildClickListener() 
{    
    public boolean onChildClick(ExpandableListView parent, 
     View v, int groupPosition, int childPosition, long id) 
    { 
     // your code... 
    } 
} 
+2

risposta irrilevante .... –

Problemi correlati