0

ExpandableListView 几乎是空的。我真的不知道是什么浪费了这么多内存。大量滚动(上下约 60 次)后,应用程序停止响应。我做了一些研究,发现这个错误只发生在 targetSdkVersions 8 到 13 中。版本 14 及更高版本一切都很好。我的应用程序为 ViewPager 使用 android 支持库 v4 和 ViewPagerIndicator。这是一个讨厌的错误,还是我的代码有问题?

package com.test.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends Activity{

    public ExpandableListAdapter mAdapter;  
    final String[] page_titles = new String[]{"page1","page2"}; 


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main_activity);

        //Instantiating the adapter
        myAdapter mAdapter = new myAdapter(this);

        //instantiate the Views
        ViewPager mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        TabPageIndicator mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);              
    }

    private class myAdapter extends PagerAdapter{
        Context context;

        public myAdapter(Context c){
            this.context = c;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return page_titles.length;
        }

        @Override
        public boolean isViewFromObject(View v, Object o) {
            // TODO Auto-generated method stub
            return v.equals(o);
        }

        public CharSequence getPageTitle(int position) {
            return page_titles[position];
        }

        public Object instantiateItem(View pager, int position) {
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View v = inflater.inflate(R.layout.expander, null, false);
            ExpandableListView elv1 = (ExpandableListView) v.findViewById(R.id.elv1);

            MainActivity.this.mAdapter = new BaseExpandableListAdapter() {

                @Override
                public Object getChild(int groupPosition, int childPosition) {
                    return null;
                }

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

                @Override
                public int getChildrenCount(int groupPosition) {
                    return 1;                                       
                }

                @Override
                public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                    View v = inflater.inflate(R.layout.childview, null, false);

                    return v;
                }

                @Override
                public Object getGroup(int groupPosition) {
                    return null;
                }

                @Override
                public int getGroupCount() {
                    return 20;
                }

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

                @Override
                public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                    View v = inflater.inflate(R.layout.twolinelistitem, parent, false);                                                                                                                                                                                 
                    return v;
                }

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

                @Override
                public boolean isChildSelectable(int groupPosition, int childPosition) {
                    return false;
                }
            };
            elv1.setAdapter(MainActivity.this.mAdapter);

            ((ViewPager) pager).addView(v, 0);
            return v;                                   
        }

        @Override
        public void destroyItem(View pager, int position, Object view) {
            ((ViewPager) pager).removeView((View) view);
        }

        @Override
        public void finishUpdate(View view) {
        }

        @Override
        public void restoreState(Parcelable p, ClassLoader c) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View view) {
        }

    }



}
4

1 回答 1

2

膨胀布局非常占用内存,您需要找到一种方法不要为每个列表项执行此操作,使用视图持有者类或仅在 pager 为空时膨胀。

视图持有者看起来像这样:

private static class ViewHolder {
    public LinearLayout linearLayout;
}

你会像这样存储你的布局

ViewHolder holder = new ViewHolder();
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.expander);
于 2013-01-23T02:20:03.770 回答