2011-07-14 8 views
11

Tengo un AlertDialog poblado por un ExpandableListView. La lista en sí funciona perfectamente, pero por alguna razón los clics se ignoran. Aparentemente mi manejador de clics nunca se llama.¿Por qué se ignoran los clics en mi ExpandableListView?

Este es el código:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select something"); 
ExpandableListView myList = new ExpandableListView(this); 
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter(); 
myList.setAdapter(myAdapter); 
myList.setOnItemClickListener(new ExpandableListView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> a, View v, int i, long l) { 
     try { 
      Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show(); 
     } 
     catch(Exception e) { 
      System.out.println("something wrong here "); 
     } 
    } 
}); 
builder.setView(myList); 
dialog = builder.create(); 
dialog.show(); 

Si en lugar de tratar de poblar el AlertDialog con una vista de lista sin formato, haga clic en eventos se generan sin problemas:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select Color Mode"); 

ListView modeList = new ListView(this); 
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" }; 
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray); 
modeList.setAdapter(modeAdapter); 
modeList.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
       Toast.LENGTH_SHORT).show(); 
     } 
     }); 


builder.setView(modeList); 
AlertDialog dialog1 = builder.create(); 
dialog1.show(); 

¿Cuál podría ser la razón por clic evento falla en mi ExpandableListView pero no en un ListView normal? Probablemente me esté perdiendo algo, pero no tengo idea de lo que podría ser.

Respuesta

20

Ok, la solución fue bastante simple. Como se trata de una lista ampliable, los clics de elementos son capturados por la propia lista para abrir los elementos secundarios. Por lo tanto, el controlador de eventos nunca se llama.

En su lugar, debe implementar OnChildClickListener() que, como su nombre lo indica, ¡escucha los clics de los niños!

2

Thx, también me lo estaba preguntando. Pero aún así es interesante información en la documentación:

setOnItemClickListener public void (AdapterView.OnItemClickListener l) "Registrar una devolución de llamada que se invoca cuando un elemento se ha hecho clic y el llamador prefiere recibir una posición de estilo en lugar de ListView un grupo y/o posición infantil ".

En documentos no se menciona claramente que usted debe ser capaz de establecer y manejar este oyente si quieres ...

+0

debe haber un comentario – user457015

1

En realidad se puede utilizar

  1. expandableListView.setOnGroupExpandListener para atrapar el evento al expandir
  2. expandibleListView.setOnGroupCollapseListener t o atrapar el evento al colapsar.
  3. expandableListView.setOnGroupClickListener para coger el caso tanto cuando expandir o contraer

enlace al sitio de desarrolladores:

https://developer.android.com/reference/android/widget/ExpandableListView.html

Cuestiones relacionadas