2013-01-15 66 views
0

我正在開發一個包含可擴展列表視圖的應用程序。子項目的不同佈局

在我的可擴展列表視圖中,我想爲每個子視圖設置不同的佈局。下面

我的自定義擴展列表適配器給出:

public Object getGroup(int arg0) 
{ 
    // TODO Auto-generated method stub 
    return menu[arg0]; 
} 

public int getGroupCount() 
    { 
    // TODO Auto-generated method stub 
    return menu.length; 
} 

public long getGroupId(int arg0) { 
    // TODO Auto-generated method stub 
    return arg0; 
} 

public View getGroupView(int position, boolean arg1, View view, ViewGroup parent) 
{ 
    if(view==null) 
    { 
     view = inflater.inflate(R.layout.edit_head, null); 

    } 
    TextView head = (TextView)view.findViewById(R.id.list_item_text_view_head); 
    head.setText(menu[position]); 
    return view; 
} 

public Object getChild(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public long getChildId(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public View getChildView(int groupPosition, int childPosition, boolean arg2, View childView, 
     ViewGroup parent) 
{ 
    if(childView==null) 
    { 
     childView = inflater.inflate(R.layout.edit_child, null, false); 
    } 

    return childView; 
} 

但單擊列表不顯示其子上。

回答

2

你可以創建你自己的適配器:

public class ExpandableAdapter extends BaseExpandableListAdapter { 

     @SuppressWarnings("unused") 
     private Context context; 
     private ArrayList<String> groups; 
     private ArrayList<ArrayList<Expand>> expand; 
     private LayoutInflater inflater; 

     public ExpandableAdapter(Context context, 
          ArrayList<String> groups, 
          ArrayList<ArrayList<Expand>> expand) { 
      this.context = context; 
      this.groups = groups; 
      this.expand = expand; 
      inflater = LayoutInflater.from(context); 
     } 
     public Object getChild(int groupPosition, int childPosition) { 
      return expand.get(groupPosition).get(childPosition); 
     } 

     public long getChildId(int groupPosition, int childPosition) { 
      return (long)(groupPosition*50+childPosition); 
     } 
     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
       View convertView, ViewGroup parent) { 
      View v = null; 
      if(convertView != null) 
       v = convertView; 
      else 
       v = inflater.inflate(R.layout.child_row, parent, false); 
      Expand c = (Expand)getChild(groupPosition, childPosition); 
      TextView color = (TextView)v.findViewById(R.id.grp_child_primero); 
      if(color != null) 
       color.setText(c.getTitulo()); 
      TextView rgb = (TextView)v.findViewById(R.id.grp_child_segundo); 
      if(rgb != null) 
       rgb.setText(c.getDato()); 
      return v; 
     } 

     public int getChildrenCount(int groupPosition) { 
      return expand.get(groupPosition).size(); 
     } 
     public Object getGroup(int groupPosition) { 
      return groups.get(groupPosition);   
     } 
     public int getGroupCount() { 
      return groups.size(); 
     } 

     public long getGroupId(int groupPosition) { 
      return (long)(groupPosition*50); 
     } 
     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
                      ViewGroup parent) { 
      View v = null; 
      if(convertView != null) 
       v = convertView; 
      else 
       v = inflater.inflate(R.layout.group_row, parent, false); 
      String gt = (String)getGroup(groupPosition); 
      TextView expandGroup = (TextView)v.findViewById(R.id.grp_child_primero); 
      if(gt != null) 
       expandGroup.setText(gt); 
      return v; 
     } 
     public boolean hasStableIds() { 
      return true; 
     } 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 
     public void onGroupCollapsed (int groupPosition) {} 
     public void onGroupExpanded(int groupPosition) {} 

    } 

有了一個XML的行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/negro"> 

    <TextView android:id="@+id/grp_child_primero" 
     android:focusable="false" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:background="@drawable/negro"/> 

    <TextView android:id="@+id/grp_child_segundo" 
     android:focusable="false" 
     android:textSize="18sp" 
     android:textStyle="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:background="@drawable/negro"/> 

</LinearLayout> 

與其他羣體:

<?xml version="1.0" encoding="utf- 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/negro"> 

    <TextView android:id="@+id/grp_child_primero" 
     android:layout_width="wrap_content" 
     android:paddingLeft="30dp" 
     android:textSize="20sp" 
     android:textColor="@drawable/blanco" 
     android:textStyle="normal" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:background="@drawable/negro"/> 

</LinearLayout> 

而在這之後:

ExpandAdapter youadapter = new ExpandAdapter(getApplicationContext(), 
     namesGroups, nameChilds); 
setListAdapter(youadapter); 

其中namesGroups和nameChild列出行和組的數據。 利用該方法setListAdapter,您可以將適配器設置爲ExpandableListActivity;) 最後展開類是一個簡單的POJO:

public class Expand { 
    public String titulo = null; 
    public String dato = null; 

    public Expand(String titulo, String dato) { 
     this.titulo = titulo; 
     this.dato = dato; 
    } 
    public String getTitulo() { 
     return titulo; 
    } 
    public String getDato() { 
     return dato; 
    } 
} 

看起來主要是getChildView方法(在ExpandableAdapter類)中實現,有的裏面如果你不認爲實現你想要的很複雜