2014-09-15 1 views
0

상위 수준과 하위 수준 모두에서 확인란을 사용하여 다중 수준 목록 뷰를 만들 수 있기를 원합니다.체크 박스가있는 다중 레벨 목록보기 - 청취자 클릭

ExpandableListAdapter를 사용하여 작업 목록보기를 만들었지 만, 확인란을 함께 통신하는 데 문제가 있습니다.

부모 확인란을 선택하면 모든 하위 확인란이 선택되고 선택 해제되면 반대가됩니다. 또한 부모 확인란이 선택되지 않고 자식이 선택되면 부모 확인란이 선택됩니다.

확인란의 단일 레이어에 대한 자습서가 많이 있지만 위에서 설명한 내용을 아직 찾지 못했습니다.

도움이 될 것입니다.

여기 내 ExtendableListAdapter 클래스입니다 :

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader; // header titles 
private List<Boolean> _listDataBoolean; // header checkbox values 
// child data in format of header title, child title 
private HashMap<String, List<String>> _listDataChild; 
private List<ArrayList<Boolean>> _listChildBoolean; 

public ExpandableListAdapter(Context context, List<String> listDataHeader, 
     HashMap<String, List<String>> listChildData, 
     List<Boolean> listDataBoolean, 
     List<ArrayList<Boolean>> listChildBoolean) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
    this._listDataBoolean = listDataBoolean; 
    this._listChildBoolean = listChildBoolean; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .get(childPosititon); 
} 

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

@Override 
public View getChildView(int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 

    final String childText = (String) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 

    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.lblListItem); 

    txtListChild.setText(childText); 

    CheckBox chk = (CheckBox) convertView 
      .findViewById(R.id.lblChildCheckbox); 
    chk.setChecked(_listChildBoolean.get(groupPosition).get(childPosition)); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 

    String headerTitle = (String) getGroup(groupPosition); 

    Boolean bool = _listDataBoolean.get(groupPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 

    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    CheckBox chk2 = (CheckBox) convertView 
      .findViewById(R.id.lblHeaderCheckbox); 
    chk2.setChecked(bool); 

    return convertView; 
} 

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

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

} 당신은 체크 박스에 OnCheckChangeListeners를 추가해야

+0

'부모 확인란이 선택되지 않고 자식이 선택되면 부모 확인란이 선택되어야합니다. ' 다른 아이들은 어떻게해야합니까? – greenapps

+0

그들은 그대로 남아 있어야합니다 – edbert

답변

0

.

+0

맞아요,하지만 그 사람이 그 기능 내에서 다른 확인란과 통신하는 방법을 찾도록 도와 줄 수 있기를 바랐습니다. – edbert

+0

물론 그렇게 될 것입니다. 왜이 의심? – greenapps

+0

다음 sudo 코드를 작성하는 방법을 모르겠습니다. parentOnClick() {모든 자식에 대해 : setUnChecked} else {모든 자식에 대해 : setChecked}} – edbert