2010-04-02 32 views
2

我有一个ExpandableListView在其中我想有除TextView以外的控件。显然,SimpleExandableListViewAdapter假定所有的控件都是TextView。如果不是,则会生成强制异常。适用于具有非TextView视图的ExpandableListView的适配器?

推荐的解决方案是什么?

我能想到的选项包括: - 使用其他包含的适配器。但我不知道他们是否都有相同的假设。 - 创建我自己的适配器。是否有描述合同的文档,即适配器遇到的方法调用顺序?

我期望现有适配器要求视图符合某些接口,以允许使用任何一致的视图,而不是硬编码到textview并限制它们可以使用的位置。

回答

0

BaseExpandableListAdapter

这种运作良好,如果你从这里效仿的榜样:https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.java 并使用自定义视图生成或通过代码膨胀。

副本从我的代码粘贴:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
       View convertView, ViewGroup parent) { 
      return viewContacts.inflateRow(null, viewContacts.this, getChild(groupPosition, 
        childPosition)); 

     } 
+0

没错,延长BaseExpadableListAdapter,然后覆盖其所有的方法。除了必须处理groupViews和childViews之外,它很简单。 至于除TextView以外的视图,请参阅LayoutInflater文档http://developer.android.com/guide/topics/ui/notifiers/toasts.html – Raja 2010-04-02 17:37:04

2

下面是一些代码,其实我只是写我的应用程序之一:

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; 
    } 
} 

我花了一点时间比我也喜欢弄清楚......但它确实没有我想象的那么糟糕。希望有所帮助!代码中还有一个LayoutInflater的例子。