Java ScrollView不会在聊天应用程序中向下滚动我的消息。安卓

Java ScrollView不会在聊天应用程序中向下滚动我的消息。安卓,java,android,scrollview,chat,Java,Android,Scrollview,Chat,我正在开发一个android聊天应用程序 这是我的xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:emojicon="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"&g

我正在开发一个android聊天应用程序

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:emojicon="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/welcomeChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Welcome to this chat"
        android:textAlignment="center"
        android:textColor="@color/textColor"
        android:textSize="20sp" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/messagePart"
        android:layout_below="@id/welcomeChat">

        <LinearLayout
            android:layout_gravity="bottom"
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <RelativeLayout
        android:id="@+id/messagePart"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="@color/white">

        <ImageView
            android:id="@+id/emoji_btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:padding="4dp"
            android:src="@drawable/smiley" />

        <ImageView
            android:id="@+id/submit_btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:padding="4dp"
            android:src="@android:drawable/ic_menu_send" />

        <hani.momanii.supernova_emoji_library.Helper.EmojiconEditText
            android:id="@+id/emojicon_edit_text"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignTop="@+id/emoji_btn"
            android:layout_toLeftOf="@id/submit_btn"
            android:layout_toRightOf="@id/emoji_btn"
            android:background="@drawable/edittext_corner"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:textColor="@color/black"
            android:textSize="20dp"
            emojicon:emojiconSize="28sp" />

    </RelativeLayout>


</RelativeLayout>


我还尝试了
ScrollView
。不管用。我的问题是,当我收到一些信息时,它离开了屏幕,我无法向下滚动它们。我不知道哪里出了问题<代码>列表视图将修复我的问题,但我也需要发送图像

免责声明:我是手工编写的,没有编译器,因此它可能并不完美,也不适合使用,因此请参考概念和示例/链接

解决方案是一个
Recyclerview
(或您想要的列表视图)

您的问题更多的是,您不知道如何用一个列表同时显示文本或图像

(可能是一个解决方案,但无论如何我都要在这里写下我的个人解释)

为此,您需要使用自定义适配器。你可以参考一点说明

现在,在适配器中,您有两种选择:

创建一个包含textView和ImageView的item.xml文件

大概是这样的:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="7dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardElevation="2dp"
        card_view:contentPaddingBottom="5dp"
        card_view:contentPaddingLeft="5dp"
        card_view:contentPaddingRight="5dp"
        card_view:contentPaddingTop="5dp">


        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="100dp"
            android:layout_marginEnd="45dp"
            android:layout_marginRight="45dp"
            android:text="@string/placeholder"
            android:textColor="@color/textGrey"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/iv"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            />
    </android.support.v7.widget.CardView>

</RelativeLayout>
if(check for text){
  iv.setVisibility(View.GONE);
  tv.setVisibility(View.VISIBLE);
  tv.setText(the text);
} else if(check for image){
  tv.setVisibility(View.GONE);
  iv.setVisibility(View.VISIBLE);
  //set iv src as you prefer
}else{
  //dunno what else, maybe error
}
另一个选项是创建两个不同的xml:
textMsg.xml
imgMsg.xml

现在您必须自定义
适配器的
getItemViewType

@Override
public int getItemViewType(int position) {
    if (isTextCheck) {
        return 1;
    } else {//it is img
        return 2;
    }
}
最后,在
onCreateViewHolder
中,您可以为返回的
viewType
返回不同的
视图

 @NonNull
    @Override
    public InterventionActivityFieldViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // ...
        View inflatedView = LayoutInflater.from(ctx)
                .inflate(viewType == 1 ? R.layout.textitem :R.layout.imgitem, parent, false);
        return new myViewHolder();
    }

是一个可能的viewholder示例。

或者更好,listview的使用与发送图像功能的关系如何?在聊天应用程序中,您可以发送消息、图像和其他类型的文件。在我的应用程序中,用户可以发送简单的文本和图像。注意这个问题。如果我创建一个listView,我需要为此listView创建一个item.xml,对吗?在xml中,我必须给出textView。如果我也添加imageView,那么listview中的每个项目都必须有文本和图像。我将尝试用一个答案来解释如何做到这一点:)如果这对您有帮助,请告诉我!