2010-04-16 20 views
5

Ok, así que escribí ExpandableListView y subíbase BaseExpandableListAdapter ... Todo funciona bien, excepto que no puedo hacer que las vistas secundarias se enfoquen cuando se hace clic. Si uso trackball todo funciona bien. Pero si intento hacer clic en un niño, no obtengo ningún comentario.ExpandableListView elementos secundarios no se enfocan cuando se toca

He intentado configurar android: enfocable, android: focusableInTouchMode y android: clicable (y también he intentado configurar esto por código) pero no puedo hacer que nada funcione. ¿Algunas ideas?

Aquí está mi código de adaptador:

public class ExpandableAppAdapter extends BaseExpandableListAdapter 
{ 
    private PackageManager m_pkgMgr; 
    private Context m_context; 
    private List<ApplicationInfo> m_groups; 
    private List<List<ComponentName>> m_children; 

    public ExpandableAppAdapter(Context context, List<ApplicationInfo> groups, List<List<ComponentName>> children) 
    { 
     m_context = context; 
     m_pkgMgr = m_context.getPackageManager(); 
     m_groups = groups; 
     m_children = children; 
    } 

    @Override 
    public Object getChild(int groupPos, int childPos) 
    { 
     return m_children.get(groupPos).get(childPos); 
    } 

    @Override 
    public long getChildId(int groupPos, int childPos) 
    { 
     return childPos; 
    } 

    @Override 
    public int getChildrenCount(int groupPos) 
    { 
     return m_children.get(groupPos).size(); 
    } 

    @Override 
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(m_context); 
      convertView = inflater.inflate(R.layout.expandable_app_child_row, null); 
     } 

     ComponentName child = (ComponentName)getChild(groupPos, childPos); 
     TextView txtView = (TextView)convertView.findViewById(R.id.item_app_pkg_name_id); 
     if (txtView != null) 
      txtView.setText(child.getPackageName()); 

     txtView = (TextView)convertView.findViewById(R.id.item_app_class_name_id); 
     if (txtView != null) 
      txtView.setText(child.getClassName()); 

     convertView.setFocusableInTouchMode(true); 
     return convertView; 
    } 

    @Override 
    public Object getGroup(int groupPos) 
    { 
     return m_groups.get(groupPos); 
    } 

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

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

    @Override 
    public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(m_context); 
      convertView = inflater.inflate(R.layout.expandable_app_group_row, null); 
     } 

     ApplicationInfo group = (ApplicationInfo)getGroup(groupPos); 
     ImageView imgView = (ImageView)convertView.findViewById(R.id.item_selection_icon_id); 
     if (imgView != null) 
     { 
      Drawable img = m_pkgMgr.getApplicationIcon(group); 
      imgView.setImageDrawable(img); 
      imgView.setMaxWidth(20); 
      imgView.setMaxHeight(20); 
     } 

     TextView txtView = (TextView)convertView.findViewById(R.id.item_app_name_id); 
     if (txtView != null) 
      txtView.setText(m_pkgMgr.getApplicationLabel(group)); 

     return convertView; 
    } 
    @Override 
    public boolean hasStableIds() 
    { 
     return false; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPos, int childPos) 
    { 
     return true; 
    } 
} 

Gracias de antemano!

+0

Estoy teniendo problemas similares tratando de administrar el enfoque programáticamente en un ExpandedListView. Por qué tu corrección 'wrap_content' funciona me elude. – Mark

+0

Técnicamente esto debería haber sido agregado como un comentario y no como una respuesta ... – Justin

+0

No estoy seguro de por qué funciona, pero funciona para mí – Justin

Respuesta

3

Ok, ¡finalmente lo descubrí!

Realmente tenía que ver con mi archivo XML en lugar de mi adaptador (mostré el adaptador para mostrar que estaba llamando al setFocusableInTouchMode cuando se creó la vista hija).

El tutorial que estaba usando (no recuerdo cuál era) tenía a poner la altura de los niños a:

android:layout_height="?android:attr/listPreferredItemHeight" 

Tan pronto como lo cambié a solo wrap_content todo comenzó a trabajar muy bien.

Cuestiones relacionadas