在回收站视图中的特定位置更新视图

时间:2016-01-01 01:20:40

标签: android android-adapter android-viewholder

试图做一个消息传递应用程序,如果其他用户已阅读该消息,我想要显示双重滴答。在我的聊天活动中,我有一个界面,可以在读取消息时进行监听。我希望在其他用户阅读消息时将气泡中的图像从一个刻度更改为双刻度,但是我尝试过的所有方法都不起作用。使用:

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;
    }
}

}

所以基本上我所要做的就是改变指定位置的交货状态。 帮助我解决这个问题将对我有所帮助,所以提前感谢。如果他也应该添加任何代码,请告诉我。

1 个答案:

答案 0 :(得分:1)

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

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