Android:ExpandableListView制作动态子级

时间:2012-01-19 11:39:47

标签: android expandablelistview

我有一个扩展BaseExpandableListAdapter的ExpandableListView我想动态添加ExpandableListView的子项(当一个孩子点击其他布局时出现) 请帮我。

1 个答案:

答案 0 :(得分:1)

编写Adapter类,如下所示

public class ExpandableArtsListAdapter implements ExpandableListAdapter {
    Context mContext;
    LayoutInflater mLayoutInflator;
    TopActivity top;
    Arts[] list ;
    public ImageLoader imageLoader;
    private static List<All> newData;

    public ExpandableArtsListAdapter(ToptActivity topActivity,
            Arts[] lists) {
        mContext = topActivity;
        imageLoader = new ImageLoader(topActivity);
        top =new TopActivity() ;
        list = lists;       
        mLayoutInflator=LayoutInflater.from(mContext);
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list[childPosition];
        // TODO Auto-generated method stub
        //return children[groupPosition][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
        Holder holder1 = new Holder();
        if (convertView == null) {
        convertView=mLayoutInflator.inflate(R.layout.top_child,null);

        holder1.TopAll= (TextView)convertView.findViewById(R.id.top_text1);
        holder1.Tops =(TextView)convertView.findViewById(R.id.top_text2);
        holder1.AllGridView=(GridView)convertView.findViewById(R.id.top_gridview);

        convertView.setTag(holder1);
        }
        else {
            holder1 = (Holder) convertView.getTag();
        }
        holder1.TopAll.setText("Top");



        final List<All> all= adapter.getList(arts.getId());

        holder1.AllGridView.setAdapter(new TopGridViewAdapter(mContext,all));
        holder1.Top.setText("Top ");

        holder1.AllGridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {


                }
            }
        });


        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
        // TODO Auto-generated method stub
        //return children[groupPosition].length;
    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
        // TODO Auto-generated method stub
        //return groups[groupPosition];
    } 

    @Override
    public int getGroupCount() {
        return list.length;
        // TODO Auto-generated method stub
        //return groups.length;
    }

    @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
        //write the appropriate code here

        return convertView;
    }

    @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;
    }

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        // TODO Auto-generated method stub

    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }
    class Holder {

        public TextView TopAll;
        public TextView Top;
        public GridView AllGridView;



    }

}