你好,游客 登录 注册 搜索
背景:
阅读新闻

ExpandableListView自定义Adapter -

[日期:2013-04-14] 来源:  作者: [字体: ]
public class MainActivity extends Activity implements OnChildClickListener {
    private List<Map<String, Object>> group;
    private List<List<Map<String, Object>>> children;
    private String[] groupFrom = { "group" };
    private String[] childFrom = { "child" };
    private int[] groupTo = { R.id.iv };
    private int[] childTo = { R.id.tv };
    private int[] groups = new int[] { R.drawable.i1, R.drawable.i2,
            R.drawable.i3 };
    private String[][] child = new String[][] { { "G1 C1", "G1 C2" },
            { "G2 C1", "G2 C2", "G2 C3" }, { "G3 C1" } };
    private ExpandableListView elv;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        elv = (ExpandableListView) findViewById(R.id.elv);
        group = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < groups.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("group", groups[i]);
            group.add(map);
        }
        children = new ArrayList<List<Map<String, Object>>>();
        for (int i = 0; i < groups.length; i++) {
            List<Map<String, Object>> lmap = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < child[i].length; j++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("child", child[i][j]);
                lmap.add(map);
            }
            children.add(lmap);
        }
        adapter = new MyAdapter(getApplicationContext(), group, R.layout.group,
                groupFrom, groupTo, children, R.layout.child, childFrom,
                childTo);
        elv.setAdapter(adapter);
        elv.setOnChildClickListener(this);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        finish();
        return true;
    }

}
public class MyAdapter extends BaseExpandableListAdapter {
    private List<List<Map<String, Object>>> children;
    private List<Map<String, Object>> group;
    private String[] childFrom, groupFrom;
    private int[] childTo, groupTo;
    private int clayout, glayout;
    private LayoutInflater inflater;

    public MyAdapter(Context context, List<Map<String, Object>> group,
            int glayout, String[] groupFrom, int[] groupTo,
            List<List<Map<String, Object>>> children, int clayout,
            String[] childFrom, int[] childTo) {
        inflater = LayoutInflater.from(context);
        this.group = group;
        this.glayout = glayout;
        this.groupFrom = groupFrom;
        this.groupTo = groupTo;
        this.children = children;
        this.clayout = clayout;
        this.childFrom = childFrom;
        this.childTo = childTo;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View cv;
        if (convertView == null)
            cv = newChildView(parent);
        else
            cv = convertView;
        bindChildView(cv, children.get(groupPosition).get(childPosition),
                childFrom, childTo);
        return cv;
    }

    private View newChildView(ViewGroup parent) {
        // TODO Auto-generated method stub
        return inflater.inflate(clayout, parent, false);
    }

    private void bindChildView(View cv, Map<String, Object> map,
            String[] childFrom, int[] childTo) {
        // TODO Auto-generated method stub
        TextView tv = (TextView) cv.findViewById(childTo[0]);
        tv.setText((String) map.get(childFrom[0]));
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return group.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return group.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View gv;
        if (convertView == null)
            gv = newGroupView(parent);
        else
            gv = convertView;
        bindGroupView(gv, group.get(groupPosition), groupFrom, groupTo);
        return gv;
    }

    private void bindGroupView(View gv, Map<String, Object> map,
            String[] groupFrom, int[] groupTo) {
        // TODO Auto-generated method stub
        ImageView iv = (ImageView) gv.findViewById(groupTo[0]);
        iv.setImageResource((Integer) map.get(groupFrom[0]));
    }

    private View newGroupView(ViewGroup parent) {
        // TODO Auto-generated method stub
        return inflater.inflate(glayout, parent, false);
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

 






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻