2014-03-25 77 views
2

我有一個android對象列表。我想遍歷列表並創建一個可展開的列表視圖,每個對象一個條目。我已經在xml文件中創建了ExpandableListView。所以我知道我開始:如何在Android中從對象列表創建可展開列表視圖?

ExpandableListView results = ((ExpandableListView)rootView.findViewById(R.id.results)); 

我想通過對象的列表,併爲每個對象Parent,並且每個Child將成爲主要對象的實例變量。

例如:

Parent = Object.Title 
    Child1 = Object.Taste 
    Child2 = Object.smell 
Parent1 = Object1.Title 
    Child1 = Object1.Taste 
    Child2 = Object1.smell 
+0

訪問https://開頭的github .com/tjerkw/Android-SlideExpandableListView –

回答

4

您需要創建延伸BaseExpandableListAdapter將管理子視圖和父視圖適配器。詳情請參閱this

+0

本來希望在SO上發佈一些更詳細的答案,而不是脫機頁面鏈接。 – raddevus

0

擴展這樣與BaseExpandableListAdapter和ovverride getChildView(...)和getGroupView您的自定義適配器(...):

public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
      tempChild = (ArrayList<String>) Childtem.get(groupPosition); 
      TextView text = null; 
      if (convertView == null) { 
       convertView = minflater.inflate(R.layout.childrow, null); 
      } 
      text = (TextView) convertView.findViewById(R.id.textView1); 
      text.setText(tempChild.get(childPosition)); 
      convertView.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
         Toast.makeText(activity, tempChild.get(childPosition), Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      return convertView; 
    } 

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = minflater.inflate(R.layout.grouprow, null); 
      } 
      ((CheckedTextView) convertView).setText(groupItem.get(groupPosition)); 
      ((CheckedTextView) convertView).setChecked(isExpanded); 
      return convertView; 
    } 

查看完整的示例源代碼here