Recycler视图刷新问题

时间:2016-08-31 10:44:25

标签: android android-recyclerview

我试图从recylerview中删除项目。项目从列表中删除,但它只显示列表中删除的最后一项,即列表未正确刷新。任何机构都可以告诉我可能是什么问题

 private ArrayList<SentMessageModel> message;
    private SentMessageModel mModel;
    private LayoutInflater inflater;
    private Context context;
    private ProgressDialog mDialog;
    private View view;
    private Set<String> conversationId = new HashSet<>();

    public SentMessageAdapter(Context context, ArrayList<SentMessageModel> message) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.message = message;
        mDialog = new ProgressDialog(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = inflater.inflate(R.layout.item_message_row, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
         SentMessageModel mSendModel = message.get(position);
        holder.nameText.setText(mSendModel.getConversationUsername());
        holder.msgText.setText(mSendModel.getSubject());
        holder.bodyText.setText(mSendModel.getBody());
        Glide.with(context)
                .load(ImageLoadingUtil.getProfileImageUrl(mSendModel.getSenderAvatar()))
                .error(R.drawable.logo_round)
                .into(holder.userImage);
        holder.ll_message_row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent mIntent = new Intent(context, ViewSentMessageActivity.class);
                mIntent.putExtra("sent_conversation_id", message.get(position).getConversationId());
                context.startActivity(mIntent);
            }
        });
        holder.mCheckBox.setTag(position);
        holder.mCheckBox.setChecked(conversationId.contains(String.valueOf(mSendModel.getConversationId())));
        holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                mModel = message.get(position);
                if (holder.mCheckBox.isChecked()) {
                    conversationId.add(mModel.getConversationId().toString());
                } else {
                    conversationId.remove(mModel.getConversationId().toString());
                }
                System.out.println("Print conversationId 2 " + conversationId);
            }
        });
    }

    public void selectCheckbox(boolean isCheckedState) {
        conversationId.clear();
        for (int i = 0; i < message.size(); i++) {
            if (isCheckedState) {
                conversationId.add(message.get(i).getConversationId().toString());
            } else {
                conversationId.remove(message.get(i).getConversationId().toString());
            }
        }
         notifyDataSetChanged();
    }

    public void callMoveToTrash() {
        if (conversationId.size() > 0) {
            String trimmedArray = conversationId.toString();
            trimmedArray = trimmedArray.replaceAll(" ", "");

            mDialog.setMessage("Please wait...");
            mDialog.setCancelable(false);
            mDialog.show();
            ApiManager.getService().moveToTrash(LocalStorage.getAuthToken(context), trimmedArray)
                    .enqueue(new Callback<MoveToTrahsModel>() {
                        @Override
                        public void onResponse(Response<MoveToTrahsModel> response, Retrofit retrofit) {
                            if (response.isSuccess() && response.body() != null) {
                                message.remove(mModel);
                                notifyDataSetChanged();
                                conversationId.clear();
                                mDialog.dismiss();
                            }
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            System.out.println("Print ex " + t.getMessage());
                            t.getMessage();
                            mDialog.dismiss();
                        }
                    });
        } else {
            ((MessageActivity) context).showSnackbar(context.getResources().getString(R.string.select_one_item));
        }
    }

    @Override
    public int getItemCount() {
        return message.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView nameText, msgText, bodyText;
        private RelativeLayout ll_message_row;
        private CheckBox mCheckBox;
        private ImageView userImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            nameText = (TextView) itemView.findViewById(R.id.nameTextView);
            msgText = (TextView) itemView.findViewById(R.id.subjectTextView);
            bodyText = (TextView) itemView.findViewById(R.id.bodyTextView);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.check_box);
            userImage = (ImageView) itemView.findViewById(R.id.user_image);
            ll_message_row = (RelativeLayout) itemView.findViewById(R.id.message_row);
        }
    }

2 个答案:

答案 0 :(得分:1)

使用此

private ArrayList<SentMessageModel> message;
private SentMessageModel mModel;
private LayoutInflater inflater;
private Context context;
private ProgressDialog mDialog;
private View view;
private Set<String> conversationId = new HashSet<>();

public SentMessageAdapter(Context context, ArrayList<SentMessageModel> message) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    this.message = message;
    mDialog = new ProgressDialog(context);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    view = inflater.inflate(R.layout.item_message_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
     SentMessageModel mSendModel = message.get(position);
    holder.nameText.setText(mSendModel.getConversationUsername());
    holder.msgText.setText(mSendModel.getSubject());
    holder.bodyText.setText(mSendModel.getBody());
    Glide.with(context)
            .load(ImageLoadingUtil.getProfileImageUrl(mSendModel.getSenderAvatar()))
            .error(R.drawable.logo_round)
            .into(holder.userImage);
    holder.ll_message_row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent mIntent = new Intent(context, ViewSentMessageActivity.class);
            mIntent.putExtra("sent_conversation_id", message.get(position).getConversationId());
            context.startActivity(mIntent);
        }
    });
    holder.mCheckBox.setTag(position);
    holder.mCheckBox.setChecked(conversationId.contains(String.valueOf(mSendModel.getConversationId())));
    holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            mModel = message.get(position);
            if (holder.mCheckBox.isChecked()) {
                conversationId.add(mModel.getConversationId().toString());
            } else {
                conversationId.remove(mModel.getConversationId().toString());
            }
            notifyDataSetChanged();
            System.out.println("Print conversationId 2 " + conversationId);
        }
    });
}

public void selectCheckbox(boolean isCheckedState) {
    conversationId.clear();
    for (int i = 0; i < message.size(); i++) {
        if (isCheckedState) {
            conversationId.add(message.get(i).getConversationId().toString());
        } else {
            conversationId.remove(message.get(i).getConversationId().toString());
        }
    }

}

public void callMoveToTrash() {
    if (conversationId.size() > 0) {
        String trimmedArray = conversationId.toString();
        trimmedArray = trimmedArray.replaceAll(" ", "");

        mDialog.setMessage("Please wait...");
        mDialog.setCancelable(false);
        mDialog.show();
        ApiManager.getService().moveToTrash(LocalStorage.getAuthToken(context), trimmedArray)
                .enqueue(new Callback<MoveToTrahsModel>() {
                    @Override
                    public void onResponse(Response<MoveToTrahsModel> response, Retrofit retrofit) {
                        if (response.isSuccess() && response.body() != null) {
                            message.remove(mModel);
                            notifyDataSetChanged();
                            conversationId.clear();
                            mDialog.dismiss();
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        System.out.println("Print ex " + t.getMessage());
                        t.getMessage();
                        mDialog.dismiss();
                    }
                });
    } else {
        ((MessageActivity) context).showSnackbar(context.getResources().getString(R.string.select_one_item));
    }
}

@Override
public int getItemCount() {
    return message.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView nameText, msgText, bodyText;
    private RelativeLayout ll_message_row;
    private CheckBox mCheckBox;
    private ImageView userImage;

    public MyViewHolder(View itemView) {
        super(itemView);
        nameText = (TextView) itemView.findViewById(R.id.nameTextView);
        msgText = (TextView) itemView.findViewById(R.id.subjectTextView);
        bodyText = (TextView) itemView.findViewById(R.id.bodyTextView);
        mCheckBox = (CheckBox) itemView.findViewById(R.id.check_box);
        userImage = (ImageView) itemView.findViewById(R.id.user_image);
        ll_message_row = (RelativeLayout) itemView.findViewById(R.id.message_row);
    }
}

答案 1 :(得分:0)

当您从用于填充min-width适配器的列表中删除某个项目时(在这种情况下,当项目被移除或添加到您的RecyclerView时),您需要执行以下操作:

ArrayList<SentMessageModel

您可以从初始化适配器类的同一个类中调用它。