2013-08-03 62 views
1

我是Android新手。我想創建一個可擴展的ListView,其中1行具有自定義信息,必須在manin線程中指定,而不是在佈局中進行硬編碼。帶有自定義組頭的Android ExpandableListView

我想要一個圖像和2個文字瀏覽。我想我需要一個自定義的佈局文件,但我不確定在代碼中如何調用它。請協助。

以下是我的自定義適配器內的GropView fxn。我想情況下0加載自定義佈局文件

public View getGroupView(int groupPosition, boolean arg1, View convertView, 
     ViewGroup arg3) { 

    String laptopName = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.group_item, null); 
    } 
    TextView item = (TextView) convertView.findViewById(R.id.laptop); 
    item.setTypeface(null, Typeface.BOLD); 
    item.setText(laptopName); 

    this.context = (Activity) context; 
    switch (groupPosition) { 
    case 0: 
     convertView.setBackgroundColor(this.context.getResources() 
       .getColor(R.color.dark_blue)); 
     convertView.inflate(R.layout.first_row_layout, 0, arg3); 
     break; 
    case 1: 
     convertView.setBackgroundColor(this.context.getResources() 
       .getColor(R.color.purple)); 
     break; 
    case 2: 
     convertView.setBackgroundColor(this.context.getResources() 
       .getColor(R.color.green)); 
     break; 
    default: 
     break; 
    } 

    return convertView; 
} 

回答

0

在你的代碼中定義的佈局爲您的項目組中的位置是這樣的:

if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.group_item, null); 
    } 

在你的情況,你正在使用的佈局 - [R .layout.group_item爲您的組項目。如果你想爲每個組的位置加載不同的佈局,你需要將這部分代碼放在switch-case中。你必須小心你的ListView中的圖像,更多關於這裏的信息:

http://developer.android.com/training/improving-layouts/smooth-scrolling.html