How to add image in expandable List in parent in android?

前端 未结 5 2166
傲寒
傲寒 2020-11-27 02:29

And is it possible to customize the child in expandable list?

5条回答
  •  旧时难觅i
    2020-11-27 03:26

    You can try making your own list adapter that extends BaseExpandableListAdapter just like described in documentation.

    Then override getGroupView(..) (for parent item, or getChildView for child item) function and in this function you can inflate your own layout xml.

    something like this:

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) 
            {
                View v = convertView;
                if (v == null) {
        //sender is activity from where you call this adapter. Set it with construktor.
                    LayoutInflater vi = (LayoutInflater)sender.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                    v = vi.inflate(R.layout.row, null);
                }
    //children = arraylists of Child 
                Child c = children.get(childPosition);
                if (c != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
                        if (tt != null) {
                              tt.setText(c.text1);                            }
                        if(bt != null){
                              bt.setText(c.text2);
                        }
                        if (icon != null) 
                        {
                            icon.setImageResource(R.drawable.rowicon);
                        }
                }
                return v;
            }
    

    layout xml :

    
    
        
        
            
            
        
    
    

提交回复
热议问题