当我连续上下滚动时,Recyclerview物品的高度会发生变化

时间:2019-02-23 04:51:18

标签: android mobile android-recyclerview

在我的一个应用程序中,我试图使用recyclerview来显示内容。我的内容就像聊天记录一样(可能是单行文本或多行文本)。要实现此目的,我使用了带有Textview高度作为wrap_content的recyclerview来根据内容调整高度。这是第一次加载正常。但是,当我上下滚动时,我会为某些项目增加一些空白。 (我强烈怀疑这个额外的空间是否用于较早的物品高度)。

例如:我有一个recyclerview列表,每行中有1,2,3,4,5 ..行文本。当我连续向下滚动时,行/项目的高度正在发生变化,例如1行文本项获得5行文本空间,而5行文本项获得1行文本。

请在下面找到我的适配器代码。

public class ConversationMessagesAdapterNew extends RecyclerView.Adapter {
private ConversationDetailActivityNew context;
private LayoutInflater layoutInflater;
private Utils utils;
private List<Message> messages = null;
private SessionManagement mSessionManage;
private int loginId = 0, attachmentsCount = 0, gifCount = 0;
private String mProfilePicId, mFname, mLname, fname = "", lname = "";
private ComposeDetails composeDetails;
private JSONArray attachmentArrayObj = new JSONArray();

public ConversationMessagesAdapterNew(ConversationDetailActivityNew context) {
    this.context = context;
    this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.utils = new Utils(context);
    setHasStableIds(true);
    this.mSessionManage = new SessionManagement(context, Utils.SHARED_PREF);
    loginId = Integer.parseInt(mSessionManage.getString("userId"));
    this.mProfilePicId = mSessionManage.getString("profilePicId");
    this.mFname = mSessionManage.getString("firstName");
    this.mLname = mSessionManage.getString("lastName");
}

public void setMessages(List<Message> messagesList) {
    this.messages = messagesList;

    /*if (messagesList == null) {
        if (this.messages != null) {
            this.messages.clear();
            this.messages = null;
        }
    } else {
        if (this.messages != null && this.messages.size() > 0)
            this.messages.addAll(messagesList);
        else
            this.messages = messagesList;
    }*/
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.conversation_messages_list_row_item_longpress, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    final ViewHolder messagesListViewHolder = (ViewHolder) holder;
    Message message = messages.get(position);
    messagesListViewHolder.mMainLayout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            (context).loadMoreButtonlayout(message);
            return true;
        }
    });
    try {
        String picId = "";
        composeDetails = (context).getProfilePicId(String.valueOf(message.getSenderId()));
        if (composeDetails != null) {
            fname = composeDetails.getFristName();
            lname = composeDetails.getLastName();
        }
        if (message.getSenderId() == loginId) {
            picId = mProfilePicId;
        } else {
            //picId = ((ConversationDetailActivity)context).getProfilePicId(String.valueOf(message.getSenderId()));
            if (composeDetails != null) {
                picId = composeDetails.getProfilePicture();
                fname = composeDetails.getFristName();
                lname = composeDetails.getLastName();
            }
            mFname = fname;
            mLname = lname;
        }

        if (!picId.equals("null")) {
            messagesListViewHolder.iv_user_profile.setVisibility(View.VISIBLE);
            messagesListViewHolder.tv_lettersText.setVisibility(View.GONE);
            messagesListViewHolder.mCardView.setCardBackgroundColor(Color.parseColor("#EFEFEF"));
            String url = String.format("%s%s", Constants.mDisplayProfilePicUrl, picId);
            Picasso.with(context).load(url).placeholder(R.drawable.user_default).into(messagesListViewHolder.iv_user_profile);
        } else {
            messagesListViewHolder.iv_user_profile.setVisibility(View.GONE);
            messagesListViewHolder.tv_lettersText.setVisibility(View.VISIBLE);
            messagesListViewHolder.mCardView.setCardBackgroundColor(Color.parseColor("#77B633"));
            String name = getName(mFname, mLname);
            messagesListViewHolder.tv_lettersText.setText(name);
        }

        if (message.getMessageType().equals("OFFNETEMAIL")) {
            messagesListViewHolder.tv_sender_name.setTextColor(Color.parseColor("#2C303E"));
        } else {
            messagesListViewHolder.tv_sender_name.setTextColor(Color.parseColor("#276eb6"));
        }

        String sendName = message.getSenderName();
        if (message.getMetric().getFlags().getCount() > 0 ||
                message.getMetric().getLikes().getCount() > 0) {
            if (sendName.length() > 15) {
                sendName = sendName.substring(0, 15) + "..";
            }
        } else {
            if (sendName.length() > 18) {
                sendName = sendName.substring(0, 18) + "..";
            }
        }
        messagesListViewHolder.tv_sender_name.setText(sendName);
        messagesListViewHolder.tv_created_time.setText(utils.getTimeStampByString(message.getCreatedOn()));

        String content = message.getContent();

        // Attachment parsing
        String prtnAttachment = "\\<attachment:(.*?)\\>";
        Pattern ptr = Pattern.compile(prtnAttachment);

        Matcher m = ptr.matcher(content);
        while (m.find()) {
            String[] fileStr = m.group(0).toString().split(":");
            if (fileStr.length > 0) {
                JSONObject file = new JSONObject();
                file.put("fileId", fileStr[1].toString());
                file.put("fileName", fileStr[2].toString());
                file.put("fileType", fileStr[3].toString());
                file.put("randomFileName", fileStr[4].toString());
                file.put("fileSize", fileStr[5].toString());
                attachmentArrayObj.put(file);
                attachmentsCount++;
            } else {
                //attachmentsFlag = false;
                // imageFlag = false;
                attachmentsCount = 0;
            }
        }
        // Emoji / Mentions parsing
        String ptrsEmoji = "\\<:(.*?)\\>";
        Pattern ptrEmoji = Pattern.compile(ptrsEmoji);

        Matcher em = ptrEmoji.matcher(content);
        while (em.find()) {
            String[] emojiStr = em.group(0).split(":");
            if (emojiStr.length > 0) {
                if (emojiStr[1].contains("mention-everyone")) {
                    String parsedStr = "<font color='#1F6EB7'>" + "@everyone" + "</font>";
                    content = content.replaceAll(em.group(0), parsedStr);
                } else if (emojiStr[1].contains("mention-")) {
                    String[] ms = emojiStr[1].split("-");
                    String parsedStr = "<font color='#1F6EB7'>" + "@" + ms[1] + "</font>";
                    content = content.replaceAll(em.group(0), parsedStr);
                } else {
                    String parsedStr = "&#x" + emojiStr[1];
                    content = content.replaceAll(em.group(0), parsedStr);
                }
            }
        }

        if (content.length() > 0) {
            messagesListViewHolder.tv_message.setVisibility(View.VISIBLE);
            messagesListViewHolder.tv_message.setText(Html.fromHtml(content));
        } else {
            messagesListViewHolder.tv_message.setVisibility(View.GONE);
        }

        if (attachmentsCount > 0) {
            messagesListViewHolder.tv_message.setVisibility(View.GONE);
            messagesListViewHolder.mAttachLayout.setVisibility(View.VISIBLE);
            messagesListViewHolder.tv_attachments.setText(String.valueOf(attachmentsCount + gifCount) + " Attachments.");
            attachmentsCount = 0;
        } else {
            attachmentsCount = 0;
            messagesListViewHolder.tv_message.setVisibility(View.VISIBLE);
            messagesListViewHolder.mAttachLayout.setVisibility(View.GONE);
        }

        // Likes and Flag sectin
        if (message.getMetric().getLikes().getCount() > 0) {
            messagesListViewHolder.iv_likes_icon.setVisibility(View.VISIBLE);
            messagesListViewHolder.tv_likes_count.setVisibility(View.VISIBLE);
            messagesListViewHolder.tv_likes_count.setText(String.valueOf(message.getMetric().getLikes().getCount()));
        } else {
            messagesListViewHolder.iv_likes_icon.setVisibility(View.GONE);
            messagesListViewHolder.tv_likes_count.setVisibility(View.GONE);
        }

        if (message.getMetric().getFlags().getCount() > 0) {
            messagesListViewHolder.iv_flag_icon.setVisibility(View.VISIBLE);
        } else {
            messagesListViewHolder.iv_flag_icon.setVisibility(View.GONE);
        }

        messagesListViewHolder.mAttachLayout.setOnClickListener(v -> {
            Intent attachmentIntent = new Intent(context, AttachmentsView.class);
            attachmentIntent.putExtra("contentAttachment", message.getContent());
            context.startActivity(attachmentIntent);
        });

        messagesListViewHolder.mAttachLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                (context).loadMoreButtonlayout(message);
                return true;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Override
public long getItemId(int position) {
    Message message = messages.get(position);
    return message.getId();
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    LinearLayout mMainLayout;
    ImageView iv_user_profile, iv_likes_icon, iv_flag_icon;
    TextView tv_sender_name, tv_created_time, tv_message, tv_lettersText, tv_attachments, tv_likes_count;
    CardView mCardView;
    FrameLayout mMoreLayout;
    RelativeLayout mAttachLayout;
    View mDummyLongPressLayout;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        mMainLayout = itemView.findViewById(R.id.longpress_layout_detailpage);

        mDummyLongPressLayout = itemView.findViewById(R.id.DummyLongPressLayout);

        mMoreLayout = itemView.findViewById(R.id.delete_layout);

        iv_user_profile = itemView.findViewById(R.id.iv_user_profile);

        tv_sender_name = itemView.findViewById(R.id.tv_sender_name);
        tv_sender_name.setTypeface(utils.mRobotoBold);

        tv_created_time = itemView.findViewById(R.id.tv_created_time);
        tv_created_time.setTypeface(utils.mRobotoRegular);

        tv_message = itemView.findViewById(R.id.tv_message);
        tv_message.setTypeface(utils.mRobotoRegular);

        tv_lettersText = itemView.findViewById(R.id.tv_lettersText);
        tv_lettersText.setTypeface(utils.mRobotoRegular);

        mCardView = itemView.findViewById(R.id.card_view);

        mAttachLayout = itemView.findViewById(R.id.AttachmentsLayout);
        mAttachLayout.setVisibility(View.GONE);

        tv_attachments = itemView.findViewById(R.id.tv_attachments);

        tv_likes_count = itemView.findViewById(R.id.tv_likes_count);
        iv_likes_icon = itemView.findViewById(R.id.iv_like_icon);
        iv_flag_icon = itemView.findViewById(R.id.iv_flag_icon);
    }
}

private String getName(String fname, String lname) {
    if (fname.length() > 0) {
        if (!fname.equals("null")) {
            fname = fname.substring(0, 1).toUpperCase();
        } else {
            fname = "";
        }
    } else {
        fname = "";
    }
    if (lname.length() > 0) {
        if (!lname.equals("null")) {
            lname = lname.substring(0, 1).toUpperCase();
        } else {
            lname = "";
        }
    } else {
        lname = "";
    }
    return fname + lname;
}

}

0 个答案:

没有答案