Android,关于滚动项目位置问题的列表视图
Android, Listview on Scroll Item position issue
我遇到了问题。
我正在处理 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;
}
}
每当加载列表视图时,您都需要考虑所有场景。我的意思是,如果您使用 if 条件为 textview 设置文本,则必须编写 else 条件并且需要为该 textview 编写操作。如果您不考虑其他部分,那么您就会遇到这个问题。当你滚动时数据被改变。
在您的情况下,您使用的是以下代码
if (inComingMessage.equals("1")) {
holder.txtDateTime.setText("Seen: "
+ convertDate(chatMessage.getDateSent() * 1000));
inComingMessage = "0";
}
但是这里没有其他部分。因此,当此 if 条件失败时,数据将更改。所以写你的其他部分。
这是一个场景。试试这个。
我遇到了问题。 我正在处理 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;
}
}
每当加载列表视图时,您都需要考虑所有场景。我的意思是,如果您使用 if 条件为 textview 设置文本,则必须编写 else 条件并且需要为该 textview 编写操作。如果您不考虑其他部分,那么您就会遇到这个问题。当你滚动时数据被改变。
在您的情况下,您使用的是以下代码
if (inComingMessage.equals("1")) {
holder.txtDateTime.setText("Seen: "
+ convertDate(chatMessage.getDateSent() * 1000));
inComingMessage = "0";
}
但是这里没有其他部分。因此,当此 if 条件失败时,数据将更改。所以写你的其他部分。
这是一个场景。试试这个。