拖动PullToRefresh ListView时内存泄漏

时间:2015-11-02 03:28:04

标签: android listview memory-leaks

问题在于,当我伸展pullToRefreshLiew(类似于listview)时,应用程序占用的内存将逐步增加。我从互联网上尝试过很多方法,但这个bug仍然存在。 当我伸展pullToRefreshLiew时,内存将增加到90m 逐渐从50m开始。

这是我的记忆监控器:

enter image description here

以下是我的代码:(我省略了导入代码) 如果你需要我的xml代码,我也会粘贴它们。我非常感谢你的帮助。

public class YuerjingActivity extends CommonActivity implements    View.OnClickListener {
private RelativeLayout back;
private int baseid;
private PullToRefreshListView pullToRefreshListView;
private ArrayList<ForumListItem> forumListItems = new  ArrayList<ForumListItem>();
private MyAdapter myAdapter;
private LinearLayout noTip;
private RelativeLayout postForum;


static class ViewHolder1 {
    TextView title;
    TextView line;
}

static class ViewHolder2 {
    private ImageView avatar;
    private TextView name;
    private TextView time;
    private ImageView huo;
    private ImageView jing;
    private TextView title;
    private LinearLayout pics;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;
    private TextView replynum;
}

protected class MyAdapter extends BaseAdapter {
    public MyAdapter() {
    }

    public int getCount() {
        return forumListItems.size();
    }

    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int t = getItemViewType(position);
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;

        ForumListItem forumListItem = forumListItems.get(position);

        if (convertView == null) {
            if (t == 1) {
                convertView = getLayoutInflater().inflate(R.layout.item_top, null);
                viewHolder1 = new ViewHolder1();

                viewHolder1.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder1.line = (TextView) convertView.findViewById(R.id.top_line);

                convertView.setTag(viewHolder1);
            } else {
                convertView = getLayoutInflater().inflate(R.layout.common_forum_item, null);
                viewHolder2 = new ViewHolder2();

                viewHolder2.avatar = (ImageView) convertView.findViewById(R.id.avatar);
                viewHolder2.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder2.time = (TextView) convertView.findViewById(R.id.time);
                viewHolder2.jing = (ImageView) convertView.findViewById(R.id.jing);
                viewHolder2.huo = (ImageView) convertView.findViewById(R.id.huo);
                viewHolder2.pics = (LinearLayout) convertView.findViewById(R.id.pics);
                viewHolder2.img1 = (ImageView) convertView.findViewById(R.id.img1);
                viewHolder2.img2 = (ImageView) convertView.findViewById(R.id.img2);
                viewHolder2.img3 = (ImageView) convertView.findViewById(R.id.img3);
                viewHolder2.img4 = (ImageView) convertView.findViewById(R.id.img4);
                viewHolder2.replynum = (TextView) convertView.findViewById(R.id.replynum);
                viewHolder2.title = (TextView) convertView.findViewById(R.id.title);

                convertView.setTag(viewHolder2);
            }
        } else {
            if (t == 1) {
                viewHolder1 = (ViewHolder1) convertView.getTag();
            } else {
                viewHolder2 = (ViewHolder2) convertView.getTag();
            }
        }

        if (t == 1) {
            viewHolder1.title.setText(forumListItem.getTitle());

            if (position + 1 < getCount()) {
                if (forumListItems.get(position + 1).getIs_top() == 0) {
                    viewHolder1.line.setVisibility(View.VISIBLE);
                } else {
                    viewHolder1.line.setVisibility(View.GONE);
                }
            } else {
                viewHolder1.line.setVisibility(View.GONE);
            }


        } else {
            if (forumListItem.getAvatar() == null) {
                viewHolder2.avatar.setImageResource(R.drawable.user);
            } else {
                ImageLoader.getInstance().displayImage(forumListItem.getAvatar(), viewHolder2.avatar, UIL.getOption3());
            }

            viewHolder2.name.setText(forumListItem.getName());
            viewHolder2.time.setText(DateUtils.toNow(forumListItem.getTime()));
            viewHolder2.title.setText(forumListItem.getTitle());
            if (forumListItem.getIs_hot() == 1) {
                viewHolder2.huo.setVisibility(View.VISIBLE);
            } else {
                viewHolder2.huo.setVisibility(View.GONE);
            }
            if (forumListItem.getIs_rec() == 1) {
                viewHolder2.jing.setVisibility(View.VISIBLE);
            } else {
                viewHolder2.jing.setVisibility(View.GONE);
            }
            viewHolder2.replynum.setText(forumListItem.getReplynum());

            viewHolder2.img1.setVisibility(View.GONE);
            viewHolder2.img2.setVisibility(View.GONE);
            viewHolder2.img3.setVisibility(View.GONE);
            viewHolder2.img4.setVisibility(View.GONE);

            for (int i = 0; i < forumListItem.pics.size(); i++) {
                if (i == 0) {
                    viewHolder2.pics.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(forumListItem.pics.get(i), viewHolder2.img1, UIL.getOptions(R.drawable.info_head));
                    viewHolder2.img1.setVisibility(View.VISIBLE);
                }
                if (i == 1) {
                    ImageLoader.getInstance().displayImage(forumListItem.pics.get(i), viewHolder2.img2, UIL.getOptions(R.drawable.info_head));
                    viewHolder2.img2.setVisibility(View.VISIBLE);
                }
                if (i == 2) {
                    ImageLoader.getInstance().displayImage(forumListItem.pics.get(i), viewHolder2.img3, UIL.getOptions(R.drawable.info_head));
                    viewHolder2.img3.setVisibility(View.VISIBLE);
                }
                if (i == 3) {
                    ImageLoader.getInstance().displayImage(forumListItem.pics.get(i), viewHolder2.img4, UIL.getOptions(R.drawable.info_head));
                    viewHolder2.img4.setVisibility(View.VISIBLE);
                }
            }
        }

        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return forumListItems.get(position).getIs_top();
    }
}

private static class ForumListItem {
    private String avatar;
    private String name;
    private String time;
    private int is_hot;
    private int is_rec;
    private int has_pic;
    private int is_top;
    private String title;
    private String replynum;
    private int id;
    public ArrayList<String> pics = new ArrayList<String>();

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getIs_top() {
        return is_top;
    }

    public void setIs_top(int is_top) {
        this.is_top = is_top;
    }

    public int getHas_pic() {
        return has_pic;
    }

    public void setHas_pic(int has_pic) {
        this.has_pic = has_pic;
    }

    public int getIs_rec() {
        return is_rec;
    }

    public void setIs_rec(int is_rec) {
        this.is_rec = is_rec;
    }

    public int getIs_hot() {
        return is_hot;
    }

    public void setIs_hot(int is_hot) {
        this.is_hot = is_hot;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getReplynum() {
        return replynum;
    }

    public void setReplynum(String replynum) {
        this.replynum = replynum;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_yuerjing);

    init();
}

private void init() {
    back = generateFindViewById(R.id.back);
    back.setOnClickListener(this);

    baseid = 0;
    noTip = generateFindViewById(R.id.notip);

    pullToRefreshListView = generateFindViewById(R.id.quanzi);
    myAdapter = new MyAdapter();
    pullToRefreshListView.setAdapter(myAdapter);
    pullToRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
    pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    pullToRefreshListView.setOnRefreshListener(new MyOnRefreshListener2());
    pullToRefreshListView.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (HttpUtil.isNetworkConnected(getApplicationContext())) {
                Intent intent = new Intent(YuerjingActivity.this, NewForumActivity.class);
                intent.putExtra("postid", forumListItems.get(position - 1).getId());
                startActivity(intent);
            } else {
            }
        }
    });

    postForum = generateFindViewById(R.id.xiexie);
    postForum.setOnClickListener(this);

    getList("new");
}

class MyOnRefreshListener2 implements PullToRefreshBase.OnRefreshListener2<ListView> {
    public MyOnRefreshListener2() {
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        getList("new");
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        getList("old");
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    }
}

private void getList(final String dir) {
    if (HttpUtil.isNetworkConnected(getApplicationContext())) {
        RequestQueue m_queue = Volley.newRequestQueue(getApplicationContext());

        JSONObject object = new JSONObject();
        String param = "";
        String url = "";
        try {
            if (UserInfo.getInstance().getHaslogin(getApplicationContext()) == -1 || UserInfo.getInstance().getHaslogin(getApplicationContext()) == 0) {
                object.put("uid", 0);
            } else {
                object.put("uid", UserInfo.getInstance().getUserId(getApplicationContext()));
                object.put("token", UserInfo.getInstance().getAccessToken(getApplicationContext()));
            }

            object.put("utype", 1);
            object.put("posttype", "2");
            object.put("classid", "0");
            object.put("pagesize", 10);
            object.put("dir", dir);
            if (!dir.equals("new")) {
                object.put("baseid", baseid);
            }

            param = object.toString();
            url = Rpc.m_rpc_url + "&message=" + "USER_GET_ARTICLES3" + "&data=" + java.net.URLEncoder.encode(param, "utf-8");
            System.err.println(url);
            System.err.println(baseid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    String result = response.toString();
                    JSONObject indexData = Utils.getInfoJsonObject(result);
                    boolean flag = indexData.getBoolean("ret");
                    int flagCode = indexData.getInt("retCode");
                    if (!flag) {
                    } else {
                        if (flagCode == 1) {
                            if (dir.equals("new")) {
                                forumListItems.clear();
                                baseid = 0;
                            }

                            String avatar_header = indexData.getString("avatar_header");
                            JSONArray jsonArray = indexData.getJSONArray("posts");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ForumListItem forumListItem = new ForumListItem();
                                if (jsonArray.getJSONObject(i).getString("author_avatar").equals("null") || jsonArray.getJSONObject(i).getString("author_avatar") == null) {
                                    forumListItem.setAvatar(null);
                                } else {
                                    forumListItem.setAvatar(avatar_header + jsonArray.getJSONObject(i).getString("author_avatar"));
                                }
                                forumListItem.setName(jsonArray.getJSONObject(i).getString("author_name"));
                                forumListItem.setTime(jsonArray.getJSONObject(i).getString("post_time"));
                                forumListItem.setReplynum(jsonArray.getJSONObject(i).getString("replynum"));
                                forumListItem.setTitle(jsonArray.getJSONObject(i).getString("title"));
                                forumListItem.setId(jsonArray.getJSONObject(i).getInt("id"));
                                if (jsonArray.getJSONObject(i).getInt("is_hot") == 1) {
                                    forumListItem.setIs_hot(1);
                                } else {
                                    forumListItem.setIs_hot(0);
                                }
                                if (jsonArray.getJSONObject(i).getInt("has_pic") == 1) {
                                    forumListItem.setHas_pic(1);
                                } else {
                                    forumListItem.setHas_pic(0);
                                }
                                if (jsonArray.getJSONObject(i).getInt("is_rec") == 1) {
                                    forumListItem.setIs_rec(1);
                                } else {
                                    forumListItem.setIs_rec(0);
                                }
                                if (jsonArray.getJSONObject(i).getInt("is_top") == 1) {
                                    forumListItem.setIs_top(1);
                                } else {
                                    forumListItem.setIs_top(0);
                                }

                                if (forumListItem.getHas_pic() == 1 && jsonArray.getJSONObject(i).has("pics")) {
                                    JSONArray pics = jsonArray.getJSONObject(i).getJSONArray("pics");
                                    for (int j = 0; j < pics.length(); j++) {
                                        forumListItem.pics.add(avatar_header + pics.getJSONObject(j).getString("srcurl"));
                                    }
                                }
                                forumListItems.add(forumListItem);
                            }
                        }else {
                            Toast.makeText(getApplicationContext(), "retCode=" + flagCode, Toast.LENGTH_SHORT).show();
                        }

                        myAdapter.notifyDataSetChanged();
                        pullToRefreshListView.onRefreshComplete();
                        if (forumListItems.size() > 0 && forumListItems.get(forumListItems.size() - 1).getIs_top() == 0) {
                            baseid = forumListItems.get(forumListItems.size() - 1).getId();
                        } else {
                            baseid = 0;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pullToRefreshListView.onRefreshComplete();
            }
        });
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        m_queue.add(jsonObjectRequest);
        m_queue.start();
    } else {
        pullToRefreshListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshListView.onRefreshComplete();
            }
        }, 500);
    }
}
}

0 个答案:

没有答案