Android:自定义列表视图内存泄漏

时间:2015-06-28 12:13:14

标签: android memory-leaks

我为列表视图制作了自定义布局。布局有2个文本视图,一个按钮和一个图像视图。当我在listview上对布局进行充气时它工作正常,但是当我在列表视图中上下滑动时,我检查我的日志猫并发现内存泄漏,我不知道问题是什么:

这是logcat:

06-27 20:37:29.321:D / dalvikvm(9034):GC_CONCURRENT释放1749K,16%免费15831K / 18723K,暂停1ms + 10ms

06-27 20:37:32.451:D / dalvikvm(9034):GC_CONCURRENT释放175K,7%免费20124K / 21475K,暂停2ms + 18ms

06-27 20:37:35.591:D / dalvikvm(9034):GC_CONCURRENT释放45K,5%免费25948K / 27171K,暂停2ms + 12ms

我的代码:

l = (ListView) findViewById(R.id.listView1);
    CustomAdapter adapter = new CustomAdapter(this,songs);
    l.setAdapter(adapter);

CustomAdapter类:

class CustomAdapter extends ArrayAdapter<String>
{
    Context c;
    LayoutInflater li;
    List<String> names = new ArrayList<String>();
    ImageView i;
    TextView t1,t2;
    Button b;

    public CustomAdapter(Context context, List<String> resource)
    {
        super(context,R.id.textView1, resource);
        c = context;
        names = resource;           
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.layout_listview, null);

        i = (ImageView) convertView.findViewById(R.id.imageView1);
        t1 = (TextView) convertView.findViewById(R.id.textView1);
        t2 = (TextView) convertView.findViewById(R.id.textView2);
        b = (Button) convertView.findViewById(R.id.button1);

        t1.setText(songs.get(position));
        t2.setText(songs.get(position));

        b.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View arg0) 
            {

            }
        });

        return convertView;
    }

}

我用这个替换了上面的代码,但问题仍然是一样的。

public View getView(int position, View convertView, ViewGroup parent) 
    {
        pos = position;
        Log.i("in getView",Integer.toString(pos));
        //li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        li = LayoutInflater.from(getContext());
        View customView = li.inflate(R.layout.layout_listview, parent,false);

        i = (ImageView) customView.findViewById(R.id.imageView_filter);
        t1 = (TextView) customView.findViewById(R.id.textView1);
        t2 = (TextView) customView.findViewById(R.id.textView2);
        LinearLayout linearonclick = (LinearLayout) customView.findViewById(R.id.LinearlayoutOnClickListView);
        b = (Button) customView.findViewById(R.id.button_eq);

        t1.setText(names.get(position));
        t2.setText(names.get(position));

        linearonclick.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Log.i("in on Click",Integer.toString(pos));
                Toast.makeText(getContext(),songs.get(pos), Toast.LENGTH_SHORT).show();
            }
        });
        b.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getContext(), "You selected modd Button", Toast.LENGTH_SHORT).show();
            }
        });

        return customView;
    }

1 个答案:

答案 0 :(得分:0)

在getView方法中,如果新的convertView不为null,则只想给它充气。首先检查它是否为空,如果它是正确的类型,则使用它。

来自Adapter getView documentation

  

convertView :如果可能,重复使用旧视图。注意:你应该检查   在使用之前,此视图为非null且具有适当的类型。如果   无法转换此视图以显示正确的数据,   此方法可以创建新视图。异类列表可以指定   它们的视图类型数量,以便此视图始终是正确的   type(请参阅getViewTypeCount()和getItemViewType(int))。