关于Scroll Item位置问题的Android,Listview

时间:2015-05-29 05:45:23

标签: android android-listview listviewitem onscroll

我遇到了问题。 我正在进行QuickBlox私聊。我想显示发送和获取的最后一条消息的日期和时间。我实现了这一点,但问题是,当我滚动该消息列表视图日期和时间更改位置和它显示的每个滚动与其他消息而不是最后。我该如何解决这个问题? 我的BaseAdapter代码如下。

public class ChatAdapter extends BaseAdapter {

private final List<QBChatMessage> chatMessages;
private Activity context;
private static String inComingMessage = "0";
private static String outGoingMessage = "0";

public ChatAdapter(Activity context, List<QBChatMessage> chatMessages,
        String inComingMessage, String outGoingMessage) {
    this.context = context;
    this.chatMessages = chatMessages;
    this.inComingMessage = inComingMessage;
    this.outGoingMessage = outGoingMessage;
}

@Override
public int getCount() {
    if (chatMessages != null) {
        return chatMessages.size();
    } else {
        return 0;
    }
}

@Override
public QBChatMessage getItem(int position) {
    if (chatMessages != null) {
        return chatMessages.get(position);
    } else {
        return null;
    }
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    QBChatMessage chatMessage = getItem(position);
    LayoutInflater vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = vi.inflate(R.layout.list_item_message, null);
        holder = createViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    QBUser currentUser = ((ApplicationSingleton) context.getApplication())
            .getCurrentUser();
    boolean isOutgoing = chatMessage.getSenderId() == null
            || chatMessage.getSenderId().equals(currentUser.getId());
    setAlignment(holder, isOutgoing);
    holder.txtMessage.setText(chatMessage.getBody());
    if (chatMessage.getSenderId() != null) {
        if (chatMessage.getSenderId().equals(currentUser.getId())) {
            if (outGoingMessage.equals("1")) {
                if (chatMessage.isRead()) {
                    holder.txtDateTime
                            .setText("Seen: "
                                    + convertDate(chatMessage.getDateSent() * 1000));
                } else {
                    holder.txtDateTime.setText(convertDate(chatMessage
                            .getDateSent() * 1000));
                }
                outGoingMessage = "0";
            }
        } else {
            if (inComingMessage.equals("1")) {
                holder.txtDateTime.setText("Seen: "
                        + convertDate(chatMessage.getDateSent() * 1000));
                inComingMessage = "0";
            }
        }
    } else {
        holder.txtDateTime
                .setText(convertDate(chatMessage.getDateSent() * 1000));
    }

    return convertView;
}

public void add(QBChatMessage message) {
    chatMessages.add(message);
}

public void add(List<QBChatMessage> messages) {
    chatMessages.addAll(messages);
}

private void setAlignment(ViewHolder holder, boolean isOutgoing) {
    if (!isOutgoing) {
        holder.contentWithBG
                .setBackgroundColor(Color.parseColor("#00aeef"));

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG
                .getLayoutParams();
        layoutParams.gravity = Gravity.RIGHT;
        holder.contentWithBG.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content
                .getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        holder.content.setLayoutParams(lp);
        layoutParams = (LinearLayout.LayoutParams) holder.txtMessage
                .getLayoutParams();
        layoutParams.setMargins(0, 10, 20, 0);
        layoutParams.gravity = Gravity.RIGHT;
        holder.txtMessage.setLayoutParams(layoutParams);

        layoutParams = (LinearLayout.LayoutParams) holder.txtDateTime
                .getLayoutParams();
        layoutParams.gravity = Gravity.RIGHT;
        holder.txtDateTime.setLayoutParams(layoutParams);
    } else {
        holder.contentWithBG
                .setBackgroundColor(Color.parseColor("#F5F5F5"));

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG
                .getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.contentWithBG.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content
                .getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        holder.content.setLayoutParams(lp);
        layoutParams = (LinearLayout.LayoutParams) holder.txtMessage
                .getLayoutParams();
        layoutParams.setMargins(0, 10, 20, 0);
        layoutParams.gravity = Gravity.LEFT;
        holder.txtMessage.setLayoutParams(layoutParams);

        layoutParams = (LinearLayout.LayoutParams) holder.txtDateTime
                .getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.txtDateTime.setLayoutParams(layoutParams);
    }
}

private ViewHolder createViewHolder(View v) {
    ViewHolder holder = new ViewHolder();
    holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
    holder.content = (LinearLayout) v.findViewById(R.id.content);
    holder.contentWithBG = (LinearLayout) v
            .findViewById(R.id.contentWithBackground);
    holder.txtDateTime = (TextView) v.findViewById(R.id.txtDateTime);
    return holder;
}

public static String convertDate(long dateInMilliseconds) {
    return DateFormat.format("hh:mm", (dateInMilliseconds)).toString();
}

private static class ViewHolder {
    public TextView txtMessage;
    public TextView txtDateTime;
    public LinearLayout content;
    public LinearLayout contentWithBG;
}
}

1 个答案:

答案 0 :(得分:1)

每当加载listview时,您需要考虑所有场景。我的意思是如果你使用if条件为textview设置文本你必须写下else条件并且需要为该textview编写动作。如果你不考虑其他部分那么你就得到了这个问题。滚动数据时会更改。

在您的情况下,您使用以下代码

if (inComingMessage.equals("1")) {
                holder.txtDateTime.setText("Seen: "
                        + convertDate(chatMessage.getDateSent() * 1000));
                inComingMessage = "0";
            }

但这里没有其他部分。因此,当此if条件失败时,数据将发生变化。所以写下你的其他部分。

这是一个场景。试试这个。