AQuery - listview中的项目位置是错误的

时间:2015-01-26 13:22:46

标签: android android-listview android-query

我写了一个自定义ArrayAdapter来显示我的listview项目。

我的物品有很多种类。如果类型是文本然后我显示TextView,如果类型是图像或视频我显示ImageView(视频缩略图)。

这是代码。

public class MessageAdapter extends ArrayAdapter<NMessage> {
public static final int ALL_MESSAGES = 0;
public static final int MEDIA_ONLY = 1;
Context context;
DatabaseActions db;
ArrayList<NMessage> items;
AQuery aq;
Profile myProfile = ProfilesSingleton.getInstance().getCurrentProfile();
LayoutInflater inflater;
int type;

public MessageAdapter(Context context, int resource, ArrayList<NMessage> objects) {
    super(context, resource, objects);
    this.context = context;
    items = objects;
    aq = new AQuery(context);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.type = type;
    db = new DatabaseActions();
}

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

    View row = convertView;
    final NMessage msg = items.get(position);
    ViewHolder holder = new ViewHolder();
    int type = getItemViewType(position);
    if (row == null) {
        if (type == 1) {
            row = inflater.inflate(R.layout.out_message, parent, false);
        } else if (type == 0) {
            row = inflater.inflate(R.layout.in_message, parent, false);
        }

        holder.avatar = (ImageView) row.findViewById(R.id.avatar);
        holder.profileName = (TextView) row.findViewById(R.id.profile_name);
        holder.text = (TextView) row.findViewById(R.id.msg_body);
        holder.image = (ImageView) row.findViewById(R.id.image_message);
        holder.video = (ImageView) row.findViewById(R.id.video_message);
        holder.time = (TextView) row.findViewById(R.id.msg_date);
        holder.location = (ImageView) row.findViewById(R.id.location_message);
        holder.audioController = (RelativeLayout) row.findViewById(R.id.audioContainer);

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    holder.video.setVisibility(View.GONE);
    holder.image.setVisibility(View.GONE);
    holder.text.setVisibility(View.GONE);
    holder.audioController.setVisibility(View.GONE);
    holder.location.setVisibility(View.GONE);

    AQuery rowAq = aq.recycle(row);
    holder.text.setText(msg.getText());
    holder.time.setText(Util.dateParser((Long.parseLong(msg.getTime()))));

    if(!msg.isSent()) {
        rowAq.id(holder.avatar).image(Util.URL_USERS_PHOTOS+msg.getPhoto_path(), false, true, 50, R.drawable.avatar_profile);
        holder.profileName.setText(msg.getName());
    } else {
        rowAq.id(holder.avatar).image(myProfile.getFullPhotoPath(), false, true, 50, R.drawable.avatar_profile);
        holder.profileName.setText("you");
    }


    switch (msg.getType()){
        case NMessage.TEXT:
            holder.text.setVisibility(View.VISIBLE);
            break;
        case NMessage.IMAGE:
            rowAq.id(holder.image).image(msg.getText(), false, true, 100, R.drawable.ic_photo).visible();
            break;
        case NMessage.VIDEO:
            String url = msg.getText().replace(msg.getText().substring(msg.getText().lastIndexOf(".")), ".jpg");
            rowAq.id(holder.video).image(url, false, true, 100, R.drawable.ic_video).visible();
            break;
        case NMessage.LOCATION:
            holder.location.setVisibility(View.VISIBLE);

            break;
        case NMessage.AUDIO:
            holder.audioController.setVisibility(View.VISIBLE);
            break;
    }

    return row;
}

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

@Override
public int getItemViewType(int position) {
    NMessage message = items.get(position);
    if (message.isSent()) {
        return 1;
    } else
        return 0;
}

private class ViewHolder {
    MediaController controller;
    RelativeLayout audioController;
    ImageView avatar,
            image,
            video,
            location;
    TextView text;
    TextView time;
    TextView profileName;
}
}

问题是某些观点未在正确的位置显示。当我滚动列表视图并向后滚动时,它们排列正确。

这是在我滚动之前。 (首次填充列表视图时) enter image description here

这是在我滚动并向后滚动之后。 imageview消失了,显示了正确的类型(文本)。 enter image description here

我到处搜索,但即使在AQuery的维基页面上似乎也找不到答案。

1 个答案:

答案 0 :(得分:0)

正如this blog post所解释的那样,Android实际上并不保证getView会收到正确的视图类型。