ExpandableListview android中每个Group标头的不同子布局



我使用了ExpandableListview,其中每个Group都有相同的child layout

现在有一种情况,根据从web服务获取的值,每个Group都有不同的子布局(有些有checkbox,有些有imageview,有些有textview

如何修改具有相同child layout 的以前的ExpandableListAdapter

可扩展列表适配器.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
        EMPTY_STATE_SET, // 0
        GROUP_EXPANDED_STATE_SET // 1
    };
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
    @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_child, null);
        }
        CheckBox chkListChild = (CheckBox) convertView.findViewById(R.id.list_child_chkbox);

        chkListChild.setText(childText);
        return convertView;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        Log.e("groupPosition", "kargr "+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);              
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
        View ind = convertView.findViewById( R.id.explist_indicator);
        if( ind != null ) {
            ImageView indicator = (ImageView)ind;
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator.setVisibility( View.INVISIBLE );
            } else {
                indicator.setVisibility( View.VISIBLE );
                int stateSetIndex = ( isExpanded ? 1 : 0) ;
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

假设不更改当前代码,正确的实现将是一个相当不错的重构,您可以这样做:

@Override
public View getChildView(int groupPosition, final int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(("IMAGE HEADER").equals(_listDataHeader.get(groupPosition))){
            convertView = infalInflater.inflate(R.layout.list_child_image, null);
        //update your views here
    }else if(("CHECKBOX HEADER").equals(_listDataHeader.get(groupPosition))){
        convertView = infalInflater.inflate(R.layout.list_child_image, null);
        //update your views here
    }
    //etc
    return convertView;       
}

正如你所看到的,这会很快变得混乱,并且不能正确地回收视图,因此会更难占用内存。它还需要对标头的字符串进行硬编码。

更好的方法是创建一个基类(而不是String),该基类包含有关视图类型以及如何将视图呈现为组的子级的信息。您的每个类型都将从此基类继承。例如:TypeCheckbox扩展了TypeListItem。

然后你的构造函数会看起来像:

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<TypeListItem>> listChildData) {
    ...
}

当您试图在任何类型的CollectionView中显示多个类型的项时,预计事情会变得复杂。

最新更新