gpt4 book ai didi

android - 在回收站 View 中的特定位置更新 View

转载 作者:行者123 更新时间:2023-11-30 01:40:38 25 4
gpt4 key购买 nike

尝试做一个消息传递应用程序,如果其他用户已阅读消息,我想显示双勾号。在我的聊天 Activity 中,我有一个界面可以监听消息是否被读取。我想在其他用户阅读消息时将气泡中的图像从一个刻度更改为两个刻度但是我尝试过的所有方法都不起作用.使用:

adapter.notifyDataSetChanged();
adapter.notifyItemChanged(position);

已尝试删除该位置的项目并再次添加,这有时有效,但大多数情况下最终会重复消息。这是我的适配器:

import android.app.Activity;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apps.norris.mobidoc.R;
import com.apps.norris.mobidoc.core.ChatService;
import com.apps.norris.mobidoc.models.MessageCache;
import com.apps.norris.mobidoc.utils.TimeUtils;
import com.quickblox.chat.QBChatService;
import com.quickblox.chat.QBPrivateChat;
import com.quickblox.chat.QBPrivateChatManager;
import com.quickblox.users.model.QBUser;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final List<MessageCache> chatMessages;
private Activity context;
private final int ME=0;
private final int OPPONENT=1;
private Typeface robotoRegular;

private QBPrivateChatManager privateChatManager;
private QBPrivateChat privateChat;

public ChatAdapter(Activity context, List<MessageCache> chatMessages,int opponentID) {
this.context = context;
this.chatMessages = chatMessages;

if(privateChatManager == null)
privateChatManager = QBChatService.getInstance().getPrivateChatManager();

privateChat = privateChatManager.getChat(opponentID);
}

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

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder videwHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

robotoRegular = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
switch (viewType){
case OPPONENT:
View v1 = inflater.inflate(R.layout.item_oppenent_chat_message,parent,false);
videwHolder = new ViewHolder1(v1);
break;
default:
View v2 = inflater.inflate(R.layout.item_my_chat_message,parent,false);
videwHolder = new ViewHolder(v2);
break;
}
return videwHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = holder.getItemViewType();
switch (viewType){
case OPPONENT:ViewHolder1 vh1 = (ViewHolder1) holder;
configureViewHolder1(vh1,position);
break;
default:ViewHolder vh = (ViewHolder) holder;
configureViewHolder(vh, position);
break;
}
}

private void configureViewHolder1(ViewHolder1 vh1, int position) {
MessageCache chatMessage = getItem(position);
vh1.getTxtInfo().setText(getTimeText(chatMessage));
vh1.getTxtMessage().setText(chatMessage.getMessage());

}

private void configureViewHolder(ViewHolder vh, int position) {
MessageCache chatMessage = getItem(position);
vh.getTxtInfo().setText(getTimeText(chatMessage));
vh.getTxtMessage().setText(chatMessage.getMessage());

if (chatMessage.isRead()) {
vh.getDelivery_status().setImageResource(R.drawable.ic_done_all_white_24dp);
}
else if (chatMessage.isDelivered()) {
vh.getDelivery_status().setImageResource(R.drawable.ic_done_white_24dp);
}
else {
vh.getDelivery_status().setImageResource(R.drawable.ic_query_builder_white_24dp);
}

}

@Override
public int getItemViewType(int position) {

MessageCache chatMessage = getItem(position);
QBUser currentUser = ChatService.getInstance().getCurrentUser();
boolean isOutgoing = chatMessage.getSenderId() == 0 || chatMessage.getSenderId()==currentUser.getId();

if (isOutgoing)
return ME;
else
return OPPONENT;
}

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

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

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

public void delete(int position){
chatMessages.remove(position);
}

private String getTimeText(MessageCache message) {
return TimeUtils.convertLongToTime(message.getTime() * 1000);
}

public class ViewHolder extends RecyclerView.ViewHolder{
public TextView txtMessage;
public TextView txtInfo;
public ImageView delivery_status;
public LinearLayout bubble_holder;

public ViewHolder(View itemView) {
super(itemView);
txtMessage = (TextView) itemView.findViewById(R.id.txtMessage);
txtInfo = (TextView) itemView.findViewById(R.id.txtInfo);
delivery_status = (ImageView) itemView.findViewById(R.id.delivery_status);
bubble_holder = (LinearLayout) itemView.findViewById(R.id.text_message_view);

//set typefaces
txtInfo.setTypeface(robotoRegular);
}

public TextView getTxtMessage() {
return txtMessage;
}

public TextView getTxtInfo() {
return txtInfo;
}

public ImageView getDelivery_status() {
return delivery_status;
}

public LinearLayout getBubbleHolder() {
return bubble_holder;
}
}

public class ViewHolder1 extends RecyclerView.ViewHolder{
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout bubble_holder;

public ViewHolder1(View itemView) {
super(itemView);
txtMessage = (TextView) itemView.findViewById(R.id.txtMessage);
txtInfo = (TextView) itemView.findViewById(R.id.txtInfo);
bubble_holder = (LinearLayout) itemView.findViewById(R.id.text_message_view);
}

public TextView getTxtMessage() {
return txtMessage;
}

public TextView getTxtInfo() {
return txtInfo;
}

public LinearLayout getBubbleHolder() {
return bubble_holder;
}
}

所以基本上我所做的就是更改指定位置的交付状态。帮我解决这个问题会对我有很大帮助,所以在此先感谢。如果他也应该添加任何代码,请告诉我。

最佳答案

我花了很长时间和很多挫折但终于能够解决它。我打电话试图错误地更新适配器。在更改之前必须从适配器中获取对我想更新的对象的引用。

MessageCache cache = adapter.getItem(position);
cache.setRead(true);
adapter.notifyItemChanged(position);
adapter.notifyDataSetChanged();

关于android - 在回收站 View 中的特定位置更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com