2011-09-24 66 views
0

我試圖在android中實現ExpandableListView。但我沒有找到任何好的教程。SimpleExpandableListAdapter的示例

我必須在父視圖上畫一個複選框和一個textview。在子視圖中,第一項包含一個複選框和一個圖像視圖,最後三個是文本視圖。任何人都可以幫助我如何延長SimpleExpandableListAdapter

+0

你試過了什麼?你能告訴我們你的努力嗎? – Howard

回答

3

找上定製的ExpandableListView的適配器的示例中的第一個地方是隨SDK提供的樣本應用程序,你可以找到在:

{sdk_root} /樣品/平臺-XX/ApiDemos /src/com/example/android/apis/view/ExpandableListView1.java

對於問題的情況下(相同的佈局所有組後面是兩個類型的佈局爲孩子行)下面你可以找到一個例子的自定義適配器。請注意,我擴展了BaseExpandableListAdapter,我這樣做是因爲擴展SimpleExpandableListAdapter僅適用於小的更改(正如其名稱所示旨在解決基本的使用場景)。

private static class CustomExpandableAdapter extends 
     BaseExpandableListAdapter { 

    // identifiers for our two types of rows, if the child rows are the same 
    // this aren't required. 
    private static final int FIRST_CHILD = 0; 
    private static final int OTHER_CHILD = 1; 

    private LayoutInflater mInflater; 
    private List<HashMap<String, Object>> mGroupData; 
    private List<ArrayList<HashMap<String, Object>>> mChildData; 

    public CustomExpandableAdapter(Context context, 
      List<HashMap<String, Object>> makeGroupData, 
      List<ArrayList<HashMap<String, Object>>> makeChildData) { 
     mInflater = LayoutInflater.from(context); 
     mGroupData = makeGroupData; 
     mChildData = makeChildData; 
    } 

    @Override 
    public int getChildType(int groupPosition, int childPosition) { 
     if (childPosition == 0) { 
      return FIRST_CHILD; // this is the first child row so return 
           // FIRST_CHILD as the type of row 
     } 
     return OTHER_CHILD; 
    } 

    @Override 
    public int getChildTypeCount() { 
     return 2; // two types of children rows 
    } 

    @Override 
    public HashMap<String, Object> getChild(int groupPosition, 
      int childPosition) { 
     return mChildData.get(groupPosition).get(childPosition); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     // if we don't have a recycled row available inflate one BASED on 
     // the type of row this child should have. 
     int type = getChildType(groupPosition, childPosition); 
     ChildViewHolder holder; 
     if (convertView == null) { 
      holder = new ChildViewHolder(); 
      switch (type) { 
      case FIRST_CHILD: 
       convertView = mInflater.inflate(
         R.layout.view_expandlistchild, parent, false); // contains only an ImageView and a CheckBox 
       holder.image = (ImageView) convertView 
         .findViewById(R.id.imageViewChild); 
       holder.check = (CheckBox) convertView 
         .findViewById(R.id.checkBoxChild); 
       break; 
      case OTHER_CHILD: 
       convertView = mInflater.inflate(
         android.R.layout.simple_list_item_1, parent, false); // contains only a TextView 
       holder.text = (TextView) convertView 
         .findViewById(android.R.id.text1); 
       break; 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (ChildViewHolder) convertView.getTag(); 
     } 
     final HashMap<String, Object> item = getChild(groupPosition, 
       childPosition); 
     // we set the data on the row based on the type of the row(so we 
     // access only the views we do have in the layout) 
     switch (type) { 
     case FIRST_CHILD: 
      holder.image.setImageResource((Integer) item.get(CHILD_IMAGE)); 
      // pass in the checked listener this as a tag so we can identify 
      // the proper data position and update it 
      holder.check.setTag(new PositionsWrapper(groupPosition, 
        childPosition)); 
      holder.check 
        .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

         @Override 
         public void onCheckedChanged(
           CompoundButton buttonView, boolean isChecked) { 
          // set the new status of the checked item 
          // otherwise the status will be erased as the 
          // user scrolls down and up 
          PositionsWrapper pw = (PositionsWrapper) buttonView 
            .getTag(); 
          mChildData.get(pw.groupPosition) 
            .get(pw.childPosition) 
            .put(CHILD_STATUS, isChecked); 
         } 
        }); 
      holder.check.setChecked((Boolean) item.get(CHILD_STATUS)); 
      break; 
     case OTHER_CHILD: 
      holder.text.setText((CharSequence) item.get(CHILD_TEXT)); 
      break; 
     } 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return mChildData.get(groupPosition).size(); 
    } 

    @Override 
    public HashMap<String, Object> getGroup(int groupPosition) { 
     return mGroupData.get(groupPosition); 
    } 

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     // normal row building like in any custom adapter 
     GroupViewHolder holder; 
     if (convertView == null) { 
      holder = new GroupViewHolder(); 
      convertView = mInflater.inflate(R.layout.view_expandlistgroup, 
        parent, false); // contains a TextView and a CheckBox 
      holder.text = (TextView) convertView 
        .findViewById(R.id.textGroup); 
      holder.check = (CheckBox) convertView 
        .findViewById(R.id.checkBoxGroup); 
      convertView.setTag(holder); 
     } else { 
      holder = (GroupViewHolder) convertView.getTag(); 
     } 
     final HashMap<String, Object> item = getGroup(groupPosition); 
     holder.text.setText((CharSequence) item.get(GROUP_TEXT)); 
     holder.check.setTag(Integer.valueOf(groupPosition)); 
     holder.check 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // again, save the new status in the data list so we 
         // keep the status as the user scrolls 
         Integer groupPosition = (Integer) buttonView 
           .getTag(); 
         mGroupData.get(groupPosition).put(GROUP_STATUS, 
           isChecked); 
        } 
       }); 
     holder.check.setChecked((Boolean) item.get(GROUP_STATUS)); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    /** 
    * Simple class that wraps two integers representing the group and child 
    * row position. 
    * 
    * @author Luksprog 
    * 
    */ 
    private static class PositionsWrapper { 

     int groupPosition; 
     int childPosition; 

     PositionsWrapper(int groupPosition, int childPosition) { 
      this.groupPosition = groupPosition; 
      this.childPosition = childPosition; 
     } 

    } 

    // basic ViewHolder classes 
    private static class GroupViewHolder { 
     TextView text; 
     CheckBox check; 
    } 

    private static class ChildViewHolder { 
     ImageView image; 
     CheckBox check; 
     TextView text; 
    } 

} 

可以找到完整的樣本here

+0

@Howard嘗試了一些示例項目,但是當我改變它們時,它們顯示了強制關閉,但現在理解了如何繪製ExpandableListView的概念。@ slukian感謝您的鏈接。 –

+0

這個答案很沒用。你應該提供一個例子 – Hersker

+1

@Hersker現在好點了嗎? – Luksprog