使用不同的项目高度向上滚动ListView

时间:2015-01-14 11:39:37

标签: android listview

我的ListView包含不同高度的项目。当我向上滚动时,它不是平滑的,并且跳跃效果会出现。所有项目都有wrap_content高度。如何顺利向上滚动?

这是适配器代码:

public class EventFeedAdapter extends ArrayAdapter<Post> {

public EventFeedAdapter(Context context, List<Post> items) {
    super(context, R.layout.item_event_feed, items);
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    if (convertView == null) {
        convertView = View.inflate(getContext(), R.layout.item_event_feed,
                null);
        ViewHolder vh = new ViewHolder();
        vh.avatar = (ImageView) convertView.findViewById(R.id.avatar);
        vh.photo = (ImageView) convertView.findViewById(R.id.photo);
        vh.textName = (TextView) convertView.findViewById(R.id.textName);
        vh.textTime = (TextView) convertView.findViewById(R.id.textTime);
        vh.progress = (ProgressBar) convertView.findViewById(R.id.progress);
        vh.buttonLike = (ImageView) convertView
                .findViewById(R.id.buttonLike);
        vh.buttonMore = (ImageView) convertView
                .findViewById(R.id.buttonMore);
        vh.textLikes = (TextView) convertView.findViewById(R.id.textLikes);
        convertView.setTag(vh);
    }

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

    Post post = getItem(position);
    String name = post.isFake() ? UserModel.get().getUser().getName()
            : post.getDisplayName();
    String avatar = post.isFake() ? UserModel.get().getUser()
            .getPictureUrl() : post.getProfileThumb();
    String mediaUrl = post.isFake() ? String.format("file://%s",
            post.getLocalMediaUri()) : post.getMediaFeed().getUrl();

    ImageLoader.getInstance().displayImage(mediaUrl, holder.photo,
            new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    holder.progress.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view,
                        FailReason failReason) {
                    holder.progress.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {
                    holder.progress.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    holder.progress.setVisibility(View.GONE);
                }
            });

    ImageLoader.getInstance().displayImage(avatar, holder.avatar);
    holder.textName.setText(name);
    Date date = post.isFake() ? post.getLocalDate() : post.getCreatedAt();
    holder.textTime.setText(DateUtils.getFormattedDate(getContext(), date));
    holder.buttonLike
            .setImageResource(post.isLocalLiked() ? R.drawable.icon_heart_filled
                    : R.drawable.icon_heart);
    if (post.isLocalLiked() && post.getLocalLikesCount() == 1)
        holder.textLikes.setText(R.string.you_like_this);
    else if (post.getLocalLikesCount() > 0)
        holder.textLikes.setText(String.format(
                getContext().getString(R.string.likes),
                post.getLocalLikesCount()));
    else
        holder.textLikes.setText("");

    holder.buttonLike.setTag(post);
    holder.buttonMore.setTag(post);

    return convertView;
}

class ViewHolder {
    ImageView photo;
    ImageView avatar;
    ImageView buttonLike;
    ImageView buttonMore;
    TextView textName;
    TextView textTime;
    ProgressBar progress;
    TextView textLikes;
}
}

如果我使用固定高度的项目滚动工作正常。

1 个答案:

答案 0 :(得分:0)

那么,你在哪里使用&#34; vh&#34;?据我了解,你必须包括一个&#34;否则&#34;陈述并使用&#34;持有人&#34;你定义的变量。否则我怀疑持有者可能是null并且每次调用getView而不管convertView是否为null。我想... 这有帮助吗?

编辑:哦,快照,我的推理是错误的...当然你确保转换视图不为空。但无论如何都要试试..

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
final ViewHolder holder;
if (convertView == null) {
    convertView = View.inflate(getContext(), R.layout.item_event_feed,
            null);


    holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
    holder.photo = (ImageView) convertView.findViewById(R.id.photo);
    holder.textName = (TextView) convertView.findViewById(R.id.textName);
    holder.textTime = (TextView) convertView.findViewById(R.id.textTime);
    holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
    holder.buttonLike = (ImageView) convertView
            .findViewById(R.id.buttonLike);
    holder.buttonMore = (ImageView) convertView
            .findViewById(R.id.buttonMore);
    holder.textLikes = (TextView) convertView.findViewById(R.id.textLikes);
    convertView.setTag(holder);
}
else
{

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


Post post = getItem(position);
String name = post.isFake() ? UserModel.get().getUser().getName()
        : post.getDisplayName();
String avatar = post.isFake() ? UserModel.get().getUser()
        .getPictureUrl() : post.getProfileThumb();
String mediaUrl = post.isFake() ? String.format("file://%s",
        post.getLocalMediaUri()) : post.getMediaFeed().getUrl();

ImageLoader.getInstance().displayImage(mediaUrl, holder.photo,
        new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String imageUri, View view) {
                holder.progress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view,
                    FailReason failReason) {
                holder.progress.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view,
                    Bitmap loadedImage) {
                holder.progress.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                holder.progress.setVisibility(View.GONE);
            }
        });

ImageLoader.getInstance().displayImage(avatar, holder.avatar);
holder.textName.setText(name);
Date date = post.isFake() ? post.getLocalDate() : post.getCreatedAt();
holder.textTime.setText(DateUtils.getFormattedDate(getContext(), date));
holder.buttonLike
        .setImageResource(post.isLocalLiked() ? R.drawable.icon_heart_filled
                : R.drawable.icon_heart);
if (post.isLocalLiked() && post.getLocalLikesCount() == 1)
    holder.textLikes.setText(R.string.you_like_this);
else if (post.getLocalLikesCount() > 0)
    holder.textLikes.setText(String.format(
            getContext().getString(R.string.likes),
            post.getLocalLikesCount()));
else
    holder.textLikes.setText("");

holder.buttonLike.setTag(post);
holder.buttonMore.setTag(post);

return convertView;

}