Android ExpanableListView getChildView不会被调用

时间:2014-03-10 06:42:37

标签: android

我正在研究以下代码。它包含多个可扩展的列表视图。一切正常,除了子孙的getchildview没被调用。我正确地获得了groupcount,groupview和childcount。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.vera.R.color;
import com.vera.navdrawer.NavDrawerItem;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class ListAdapter_SelectDevice extends BaseExpandableListAdapter {

    private Context _context;
    public ArrayList<NavDrawerItem> _listDataHeader; // header titles
    public HashMap<String, ArrayList<NavDrawerItem>> _listDataChild;

    public ListAdapter_SelectDevice(Context context, ArrayList<NavDrawerItem> listDataHeader, HashMap<String, ArrayList<NavDrawerItem>> listDataChild) {
        this._context = context;
        this._listDataHeader = listDataHeader; 
        this._listDataChild = listDataChild;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        NavDrawerItem l = this._listDataChild.get(this._listDataHeader.get(groupPosition).getTitle()).get(childPosititon);
        return l;
        /*String s = this._listDataChild.get(this._listDataHeader.get(groupPosition).getTitle()).get(childPosititon);
        return s;*/
    }

    @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) {

        NavDrawerItem child = (NavDrawerItem) getChild(groupPosition, childPosition);
        /*if (convertView == null) 
        {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(groupPosition==0)
            {
                convertView = infalInflater.inflate(R.layout.row_selectdevice_listitem, null);
            }
            else
            {
                convertView = infalInflater.inflate(R.layout.row_selectdevice_childlist, null);
            }
        }*/
        if(groupPosition==0)
        {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_selectdevice_listitem, null);
            TextView txtListChild = (TextView) convertView.findViewById(R.id.label_deviceToSelect);
            txtListChild.setText(child.getTitle());
        }
        else
        {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_selectdevice_childlist, null);
            ExpandableListView expListView = (ExpandableListView) convertView.findViewById(R.id.list_selectDevice); 
            expListView.setChildDivider(_context.getResources().getDrawable(R.drawable.unit_cell_line_spacing));
            final ArrayList<NavDrawerItem> listDataHeader = new ArrayList<NavDrawerItem>();
            final HashMap<String, ArrayList<NavDrawerItem>> listDataChild = new HashMap<String, ArrayList<NavDrawerItem>>();
            TypedArray navMenuIcons = _context.getResources().obtainTypedArray(R.array.nav_drawer_icons);           
            listDataHeader.add(new NavDrawerItem(child.getTitle(), navMenuIcons.getResourceId(1, -1), false));
            //Prepare subchilds list
            ArrayList<NavDrawerItem> subChilds = new ArrayList<NavDrawerItem>(); 
            subChilds.add(new NavDrawerItem("Sub Child 1", navMenuIcons.getResourceId(1, -1), false));
            subChilds.add(new NavDrawerItem("Sub Child 2", navMenuIcons.getResourceId(1, -1), false));

            ArrayList<NavDrawerItem> energyChilds = new ArrayList<NavDrawerItem>(); 
            energyChilds.add(new NavDrawerItem("Energy Monitoring Dimmable Lamp Module", navMenuIcons.getResourceId(1, -1), false));
            energyChilds.add(new NavDrawerItem("Home Energy Meter", navMenuIcons.getResourceId(1, -1), false));
            energyChilds.add(new NavDrawerItem("SmartSwitch", navMenuIcons.getResourceId(1, -1), false));

            if(child.getTitle().equalsIgnoreCase("energy"))
            {
                listDataChild.put(child.getTitle(), energyChilds); 
                Log.e("Check subchild", child.getTitle() + energyChilds);
            }       
            ListAdapter_SelectDeviceChild listAdapter = new ListAdapter_SelectDeviceChild(_context, listDataHeader, listDataChild);
            expListView.setAdapter(listAdapter);

            ////////////////////////////////////////
            // Listview Group click listener
            /*expListView.setOnGroupClickListener(new OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                     Toast.makeText(_context.getApplicationContext(),
                     "Group Clicked " + listDataHeader.get(groupPosition),
                     Toast.LENGTH_SHORT).show();

                    return false;
                }
            });
            // Listview Group expanded listener
            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(_context.getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Expanded",
                            Toast.LENGTH_SHORT).show();
                    listAdapter._listDataHeader.get(groupPosition).setIsSelected(true);
                    listAdapter.notifyDataSetChanged();

                }
            });
            // Listview Group collasped listener 
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(_context.getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Collapsed",
                            Toast.LENGTH_SHORT).show();
                    listAdapter._listDataHeader.get(groupPosition).setIsSelected(false);
                    listAdapter.notifyDataSetChanged();
                }
            });
            // Listview on child click listener
            expListView.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(
                            _context.getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                            listDataHeader.get(groupPosition)).get(
                                            childPosition), Toast.LENGTH_SHORT)
                            .show();
                    return false;
                }
            });*/


        }
        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        String key = this._listDataHeader.get(groupPosition).getTitle();
        return this._listDataChild.get(key).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) {
        NavDrawerItem header = (NavDrawerItem) getGroup(groupPosition); 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_selectdevice_groupitem, null);
        }

        RelativeLayout layout_selectDeviceOptions = (RelativeLayout) convertView.findViewById(R.id.layout_selectDeviceOptions);
        ImageView img_group_items = (ImageView) convertView.findViewById(R.id.img_group_items); 
        if(header.getIsSelected())
        {
            layout_selectDeviceOptions.setBackgroundColor(Color.parseColor("#FFFFE0")); 
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) 
            {
                img_group_items.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.cart));
            } else {
                img_group_items.setBackground(_context.getResources().getDrawable(R.drawable.cart));
            }
            //layout_selectDeviceOptions.setBackgroundResource(_context.getResources().getColor(R.color.yellow)); 
        }
        else
        {
            layout_selectDeviceOptions.setBackground(null); 
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) 
            {
                img_group_items.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.plus));
            } else {
                img_group_items.setBackground(_context.getResources().getDrawable(R.drawable.plus));
            }
        }
        TextView lblListHeader = (TextView) convertView.findViewById(R.id.label_group_items);
        lblListHeader.setText(header.getTitle());

        return convertView;
    }

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

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

}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.vera.R.color;
import com.vera.navdrawer.NavDrawerItem;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class ListAdapter_SelectDeviceChild extends BaseExpandableListAdapter {

    private Context _context;
    public ArrayList<NavDrawerItem> _listDataHeader; // header titles
    public HashMap<String, ArrayList<NavDrawerItem>> _listDataChild;

    public ListAdapter_SelectDeviceChild(Context context, ArrayList<NavDrawerItem> listDataHeader, HashMap<String, ArrayList<NavDrawerItem>> listDataChild) {
        this._context = context;
        this._listDataHeader = listDataHeader; 
        this._listDataChild = listDataChild;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        NavDrawerItem l = this._listDataChild.get(this._listDataHeader.get(groupPosition).getTitle()).get(childPosititon);
        return l;
        /*String s = this._listDataChild.get(this._listDataHeader.get(groupPosition).getTitle()).get(childPosititon);
        return s;*/
    }

    @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) {

        NavDrawerItem child = (NavDrawerItem) getChild(groupPosition, childPosition);
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.row_selectdevice_listitem, null);
        TextView txtListChild = (TextView) convertView.findViewById(R.id.label_deviceToSelect);
        txtListChild.setText(child.getTitle());

        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        String key = this._listDataHeader.get(groupPosition).getTitle();
        return this._listDataChild.get(key).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) {
        NavDrawerItem header = (NavDrawerItem) getGroup(groupPosition); 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.row_selectdevice_groupitem, null);
        }

        RelativeLayout layout_selectDeviceOptions = (RelativeLayout) convertView.findViewById(R.id.layout_selectDeviceOptions);
        ImageView img_group_items = (ImageView) convertView.findViewById(R.id.img_group_items); 
        if(header.getIsSelected())
        {
            layout_selectDeviceOptions.setBackgroundColor(Color.parseColor("#FFFFE0")); 
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) 
            {
                img_group_items.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.cart));
            } else {
                img_group_items.setBackground(_context.getResources().getDrawable(R.drawable.cart));
            }
            //layout_selectDeviceOptions.setBackgroundResource(_context.getResources().getColor(R.color.yellow)); 
        }
        else
        {
            layout_selectDeviceOptions.setBackground(null); 
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) 
            {
                img_group_items.setBackgroundDrawable(_context.getResources().getDrawable(R.drawable.plus));
            } else {
                img_group_items.setBackground(_context.getResources().getDrawable(R.drawable.plus));
            }
        }
        TextView lblListHeader = (TextView) convertView.findViewById(R.id.label_group_items);
        lblListHeader.setText(header.getTitle());

        return convertView;
    }

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

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

}

0 个答案:

没有答案