Listview暂时冻结

时间:2014-10-16 21:19:51

标签: android listview android-listview

Activity的Listview由自定义ArrayAdapter加载。当活动打开时,可以看到顶部的一半列表视图项目 - 这对于高度来说是可以的。但是,当向下滚动时,看到/加载第三个项目之前的那一刻,活动会暂时冻结。在第三个项目完成加载后,其他项目将完全没有冻结。什么会导致冻结?

这是getView方法(我认为没什么可疑的):

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView instanceof LinearLayout)
        return convertView;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item_overview, parent, false);

    TextView venueName = (TextView) rowView.findViewById(R.id.venueName);
    TextView requestTime = (TextView) rowView.findViewById(R.id.requestTime);
    TextView likeCount = (TextView) rowView.findViewById(R.id.likeCount);
    TextView commentCount = (TextView) rowView.findViewById(R.id.commentCount);
    TextView singerName = (TextView) rowView.findViewById(R.id.singerName);
    TextView songName = (TextView) rowView.findViewById(R.id.songName);

    venueName.setText(feeds.get(position).getVenue().getName());
    requestTime.setText(TimeUtil.getFeedItemRequestedTime(feeds.get(position).getRequested_at()));
    likeCount.setText(Integer.toString(feeds.get(position).getLikes()));
    commentCount.setText(Integer.toString(feeds.get(position).getComments()));
    singerName.setText(feeds.get(position).getSong().getArtist());
    songName.setText(feeds.get(position).getSong().getTitle());


    return rowView;
}

1 个答案:

答案 0 :(得分:2)

首先,您每次都在膨胀视图。在前几次之后,convertView已经被夸大了。此外,findViewById()是一个相当昂贵的电话。建议使用ViewHolder策略来减少呼叫。尝试更改为此并查看性能是否有所改善。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView instanceof LinearLayout)
        return convertView;

    View rowView = super.getView(position, convertView, parent);

    ViewHolder holder = (ViewHolder)convertView.getTag();

    if (holder == null) {
        holder = new ViewHolder();
        holder.venueName = (TextView) rowView.findViewById(R.id.venueName);
        holder.requestTime = (TextView) rowView.findViewById(R.id.requestTime);
        holder.likeCount = (TextView) rowView.findViewById(R.id.likeCount);
        holder.commentCount = (TextView) rowView.findViewById(R.id.commentCount);
        holder.singerName = (TextView) rowView.findViewById(R.id.singerName);
        holder.songName = (TextView) rowView.findViewById(R.id.songName);
        rowView.setTag(holder);
    }

    holder.venueName.setText(feeds.get(position).getVenue().getName());
    holder.requestTime.setText(TimeUtil.getFeedItemRequestedTime(feeds.get(position).getRequested_at()));
    holder.likeCount.setText(Integer.toString(feeds.get(position).getLikes()));
    holder.commentCount.setText(Integer.toString(feeds.get(position).getComments()));
    holder.singerName.setText(feeds.get(position).getSong().getArtist());
    holder.songName.setText(feeds.get(position).getSong().getTitle());


    return rowView;
}

private static class ViewHolder {
    TextView venueName, requestTime, likeCount, commentCount,
        singerName, songName;
}