首页 > 解决方案 > 处理聊天活动中的图像视图位置

问题描述

在我的应用程序中,当用户发送或接收图像时,如果用户滚动离开该图像并滚动回该图像,则图像变得不可见,使其最后一个位置为空,从而弄乱了视图。当用户关闭ChatActivity并重新打开它时,图像会重新出现。

所以我现在的问题是,即使用户滚动到顶部和返回,我如何才能让图像始终保持在它的位置?

这是我发送的消息持有人

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="8dp">

<TextView
    android:id="@+id/text_message_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="4dp"
    android:text="11:40"
    android:textColor="@color/colorPrimaryy"
    android:textSize="10sp"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="@+id/text_message_body"
    app:layout_constraintRight_toLeftOf="@+id/text_message_body" />


<TextView
    android:id="@+id/text_message_body"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:background="@drawable/sent_message_bubble"
    android:padding="10dp"
    android:text="hello, hello!"
    android:textColor="#080000"
    android:textSize="17sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/read_reciept"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="28dp"
    android:text="seen"
    android:textStyle="bold|italic"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text_message_body"
    app:layout_constraintVertical_bias="0.0" />

<ImageView
    android:id="@+id/message_sent_image_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_marginStart="0dp"
    android:maxWidth="240dp"
    android:background="@drawable/sent_message_bubble"
    android:padding="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:scaleType="fitEnd" />

 </android.support.constraint.ConstraintLayout>

这是消息适配器

 void bind(Messages message) {

            String user_id = message.getFrom();
            String message_type = message.getType();

            boolean seen = message.isSeen();



            if (message_type.equals("text")) {

                if (seen){
                    readReciept.setVisibility(View.VISIBLE);
                }else {
                    readReciept.setVisibility(View.INVISIBLE);
                }

                messageText.setText(message.getMessage());
                // Format the stored timestamp into a readable String using method.
                timeText.setText(DateUtils.formatDateTime(message.getTime()));
                mSentImage.setVisibility(View.INVISIBLE);
            }else {
                messageText.setVisibility(View.INVISIBLE);
                Picasso.with(mSentImage.getContext()).load(message.getMessage()).into(mSentImage);
   }

标签: javaandroidandroid-constraintlayout

解决方案


推荐阅读