【问题标题】:Android : Custom List view Memory LeakAndroid:自定义列表视图内存泄漏
【发布时间】:2015-06-28 12:13:13
【问题描述】:

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

这里是日志:

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 freed 45K, 5% free 25948K/27171K, paused 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;
    }

【问题讨论】:

标签: android memory-leaks


【解决方案1】:

在 getView 方法中,您只想在新的 convertView 不为 null 时对其进行膨胀。首先检查它是否不为空,如果它是正确的类型然后使用它。

来自Adapter getView documentation

convertView:如果可能的话,要重用的旧视图。注意:您应该检查 在使用之前,这个视图是非空的并且是适当的类型。如果 无法将此视图转换为显示正确的数据, 这个方法可以创建一个新视图。异构列表可以指定 他们的视图类型的数量,所以这个视图总是正确的 类型(参见 getViewTypeCount() 和 getItemViewType(int))。

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 2011-12-24
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    相关资源
    最近更新 更多