可扩展的ListView一次又一次地调用getGroupCount()方法

时间:2015-06-26 07:30:18

标签: android expandablelistview expandablelistadapter

我已将BaseExpandableListAdapter用于我的可扩展列表视图。它一次又一次地调用getGroupCount(),这会降低应用程序性能。 这是我的代码:

public class ExpandableCustomAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    ArrayList<MenuItem> menuItem;
    Context context;
    private Map<String, ArrayList<MenuItem>> parent;

    public ExpandableCustomAdapter(Context context, Map<String, ArrayList<MenuItem>> mParent) {
        parent = mParent;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    // counts the number of group/parent items so the list knows how many
    // times calls getGroupView() method
    public int getGroupCount() {
        int size = parent.size();

        return size;
    }

    @Override
    // counts the number of children items so the list knows how many times
    // calls getChildView() method
    public int getChildrenCount(int i) {
        List<String> copyOfKeys = new ArrayList<String>(parent.keySet());
        menuItem = parent.get(copyOfKeys.get(i));

        return menuItem.size();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<String> copyOfKeys = new ArrayList<String>(parent.keySet());
        menuItem = parent.get(copyOfKeys.get(groupPosition));
        Object obj = menuItem.get(childPosition);

        return obj;
    }

    @Override
    // in this method you must set the text to see the parent/group on the
    // list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        ParentViewHolder holder;
        System.out.println("getview:" + i);

        if (view == null)
        {
            holder = new ParentViewHolder();
            LayoutInflater infalInflater = (LayoutInflater) instance.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(
                    R.layout.bannerlayoutparentinflater, null);
            holder.backgroundRelative = (RelativeLayout) view
                    .findViewById(R.id.mainlayoutparent);

            view.setTag(holder);
        }

        else
        {
            holder = (ParentViewHolder) view.getTag();
        }

        // Beverages_Cocktails_banner
        List<String> copyOfKeys = new ArrayList<String>(parent.keySet());
        menuItem = parent.get(copyOfKeys.get(i));
        String course = menuItem.get(0).course;
        Drawable background = new BitmapDrawable(getResources(), ImageList
                .getInstance().getImageBitmap(value);
        holder.backgroundRelative.setBackground(background);

        // set Image indicator
        holder.indicator = (ImageView) view.findViewById(R.id.imageView1);

        if (b)
        {
            holder.indicator.setRotation(180);
        }

        else
        {
            Bitmap menuIndBitmapicatorBitmap = ImageList.getInstance()
                    .getImageBitmap(ImageNames.SECTION_HEADER_ARROW_IMAGE);
            holder.indicator.setImageBitmap(menuIndBitmapicatorBitmap);
            holder.indicator.setRotation(360);
        }

        // return the entire view
        return view;
    }

    @Override
    // in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view,
            ViewGroup viewGroup) {

        ChildViewHolder childHolder;

        if (view == null) {
            childHolder = new ChildViewHolder();
            inflater = LayoutInflater.from(context);
            LayoutInflater infalInflater = (LayoutInflater) instance.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(
                    R.layout.bannerlayoutchildinflater, null);
            childHolder.backgroundRelativeChild = (RelativeLayout) view
                    .findViewById(R.id.mainlayoutchild);

            view.setTag(childHolder);
        }

        else
        {
            childHolder = (ChildViewHolder) view.getTag();
        }

        //childHolder.backgroundRelativeChild.removeAllViews();

        List<String> copyOfKeys = new ArrayList<String>(parent.keySet());
        menuItem = new ArrayList<MenuItem>();
        menuItem = parent.get(copyOfKeys.get(i));

        // removeAllViews();

        CategoryCourseMenuList menuList1 = new CategoryCourseMenuList(
                instance, null, 0, menuItem);
        MenuItem menu=menuItem.get(i1);
        menuList1.addLayoutMenu(menu);

    //  menuList1.setCourseMenuList(menuItem);

        LayoutParams menuListParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        menuListParams.topMargin = Utils.getPixelValue(2);
        menuListParams.bottomMargin = Utils.getPixelValue(2);
        childHolder.backgroundRelativeChild.addView(menuList1, menuListParams);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.list_animation);

        // view.startAnimation(animation);
        // animation = null;

        // return the entire view
        return view;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

        /* used to make the notifyDataSetChanged() method work */
        super.registerDataSetObserver(observer);
    }

    @Override
    public Object getGroup(int groupPosition) {
        Object obj = parent.get(groupPosition);

        return obj;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

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

任何想法?

0 个答案:

没有答案
相关问题