2012-04-09 21 views
5

Tengo un ExpandableListView y quiero registrar la posición de grupo al hacer clic en un grupo. Desafortunadamente, el código a continuación siempre arroja 0, como si estuviera haciendo clic en el grupo número 0.Android ExpandableListView posición de grupo siempre 0

exList.setOnGroupClickListener(new OnGroupClickListener() { 

    @Override 
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
      groupPosition = ExpandableListView.getPackedPositionGroup(id); 

      Log.i("group position", groupPosition + ""); 
      return false; 
    } 

    }); 

también tengo una longclicklistener de los grupos y del niño que trabaja derecha:

exList.setOnItemLongClickListener(new OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
      if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
       groupPosition = ExpandableListView.getPackedPositionGroup(id); 
... 
} 

¿Alguna idea?

+0

¿Utiliza un adaptador personalizado para ExpandableListView? – Cata

+0

sí. Mis otros oyentes funcionan bien – erdomester

+0

bien, asegúrese de que los métodos getItem y getItemId devuelvan valores válidos y no 0 .. (en su Adaptador personalizado) también puede ver este adaptador http://developer.android.com/resources/ samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html – Cata

Respuesta

0

Use el detector OnGroupExpandListener, el parámetro del método onGroupExpand es la posición del grupo en ExpandableListView.

Al igual que:

listView = (ExpandableListView) findViewById(R.id.expandableListView); 
listView.setOnGroupExpandListener(new OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 
     Log.d(TAG, "pos " + groupPosition); 
    } 
}); 
0
@Override 
public Object getGroup(int groupPosition) { 
    Log.i(TAG, "* getGroup : groupPosition =" + groupPosition); 

    return categories[groupPosition]; 
} 

Cuando se extiende BaseExpandableListAdapter, obtendrá el método de reemplazo anteriormente. El cierre de sesión anterior muestra claramente el número de grupo cliqueado.

En mi código, categorías significa el conjunto de datos que estoy pasando a la lista desplegable como los grupos.

Tendrá otra llamada al método de anulación;

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
... 
} 

Con en ese método puede llamar de la siguiente manera;

if(getGroup(groupPosition).toString().equals("GroupName")){ 
    //Do what even you like in for the clicked group 
} 

Este es un código de trabajo y espero que pueda entenderlo.

1

su vista de lista desplegable podría estar dentro de una vista de desplazamiento. No puede haber anidado el desplazamiento.

+0

Gracias! Necesitaba esto. Simplemente no podía entender lo que estoy haciendo mal. Estaba en este caso. – sunlover3

+1

contento de ayudar ... !! :) – navalkishoreb

3

asegurarse de que tiene esto para el diseño

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


    <ExpandableListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

No se puede tener un ExpandableListView en un ScrollView.

Cuestiones relacionadas