Android Expandable Listview customization on child view
Asked Answered
W

2

0

i want to implement expandable list view with multiple child layouts. all works fine but problem om,child view..child not appear on appropriate position.... here is my code...PLZ HELP ME..

i think problem on getchildview().i not able to hold child id of each parent...

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    Button btn;
    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);

        Object childPosititon;
        if(convertView == null )
        {
        if ( getChildId(groupPosition, childPosition)!=3) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.alarm_list, null);
        }
        else if( getChildId(groupPosition, childPosition)==3)
        {

            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.vibrate, null);

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

        txtListChild.setText(childText);

    }
        return convertView;
    }

    private List<String> get(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @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);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listgroup, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        //btn=(Button)convertView.findViewById(R.id.delete);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        lblListHeader.setTag(""+groupPosition);
        OnTouchListener item = null;
        lblListHeader.setOnTouchListener(item);


        return convertView;
    }

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

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


    public int getViewTypeCount() 
    {
        return 2;
    }

    public int getItemViewType(int groupposition,int childposition) {

        if (getChildId(groupposition, childposition)==3) 
            return 0;

        //Not free
        else 
            return 1;
    }

}
Woodenhead answered 3/4, 2014 at 7:40 Comment(0)
T
1

BaseExpandableListAdapter doesn't have getViewTypeCount() and getItemViewType(). in order to have different children view types you have to override the following methods instead.

@Override
public int getChildTypeCount() {
    return 2;
}

@Override
public int getChildType(int groupPosition, int childPosition) {
if (getChildId(groupposition, childposition)==3) 
        return 0;

    //Not free
    else 
        return 1;
}
Tarton answered 7/4, 2014 at 12:27 Comment(1)
Do both child layouts have a textview with the same id?. Also, why not directly to use childPosition instead of getChildId()?Tarton
L
0

I have done this by add and remove the header view under the parent group layout. If you group layout is a LinearLayout that have orientation="vertical" like this

<LinearLayout
    android:id="@+id/group_layout_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

I add a references to the ExpandableListView in my constructor for my ExpandListAdapter so I can check if the group is expanded

 private ExpandableListView list;

    public ExpandListAdapter(Context context, ArrayList<Group> groups, ExpandableListView list) {
        this.list = list;   
    }

and then in top of the method getGroupView check if it is expandet and add or if it is not remove

public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
    final Group group = getGroup(groupPosition);

    LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inf.inflate(R.layout.group_list_row, null);
    final View childheader = inf.inflate(R.layout.child_list_header, null);

    LinearLayout groupLayout = (LinearLayout) view.findViewById(R.id.group_layout_id);

    if(list.isGroupExpanded(groupPosition)){
        groupLayout.addView(childheader, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }else{
        groupLayout.removeView(childheader);
    }
Libbey answered 15/1, 2015 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.