2011-09-24 3 views
0

안드로이드에 ExpandableListView을 구현하려고합니다. 그러나 좋은 튜토리얼을 찾지 못했습니다.SimpleExpandableListAdapter의 예제

부모보기에서 체크 상자와 텍스트 뷰를 그려야합니다. 하위보기에서 첫 번째 항목에는 이미지보기 확인란이 있고 마지막 세 항목은 텍스트보기입니다. 어느 누구도 연장 방법을 알려주십시오 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 Better now? – Hersker

+1

예를 제공해야합니까? – Luksprog