【问题标题】:Android chat listview not scrolling totallyAndroid聊天列表视图不完全滚动
【发布时间】:2018-11-13 17:00:20
【问题描述】:

我有一个聊天列表视图,它不会滚动到所有消息的末尾。我无法弄清楚为什么会这样。如果我发送一条新消息并且聊天中所有消息的长度超过一页,那么我需要滚动查看消息,我成功滚动但不足以查看所有消息。

我的布局 XML 代码在这里:

<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/cool_background3">


<ListView
    android:layout_width="wrap_content"
    android:id="@+id/messages_view"
    android:layout_weight="1"
    android:divider="#fff"
    android:layout_height="0dp"
    tools:layout_editor_absoluteX="8dp"
    app:layout_constraintTop_toBottomOf="@+id/topBar" />

<LinearLayout
    android:id="@+id/layout_chatbox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:minHeight="48dp"
    android:background="#ffffff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent">


    <EditText
        android:id="@+id/edittext_chatbox"
        android:hint="Enter message"
        android:background="@android:color/transparent"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:maxLines="6"
        />

    <Button
        android:id="@+id/button_chatbox_send"
        android:text="send"
        android:textSize="14dp"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:gravity="center"
        android:textColor="@color/myColor"
        android:layout_gravity="bottom"
        android:onClick="sendMessage"/>

</LinearLayout>

<RelativeLayout
    android:id="@+id/topBar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:minHeight="50dp"
    android:background="@color/white"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:layout_editor_absoluteY="0dp">

    <TextView
        android:id="@+id/name_of_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tomnaor"
        android:textColor="@color/myColor"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_marginStart="30dp"
        android:layout_centerVertical="true"
        />

    <TextView
        android:id="@+id/emotion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Happy Chat"
        android:textColor="#939393"
        android:textSize="14dp"
        android:layout_toEndOf="@+id/name_of_user"
        android:layout_marginStart="4dp"
        android:layout_below="@+id/name_of_user"/>

</RelativeLayout>

【问题讨论】:

  • 为什么您的列表视图高度为“0dp”?您是否以编程方式更改它?
  • @SafalSharma 这是告诉“取走其他人留下的地方”的方式。
  • @vincrichaud 是的,我明白了,但问题可能是由于没有以编程方式正确更改它引起的。谢谢
  • @SafalSharma 要设置 hieght="0dp" 是正确的,您以后不必以编程方式更改它。它告诉系统始终调整此视图的高度以占用剩余空间。
  • @vincrichaud 谢谢,以前从来不知道这个:)

标签: java android xml android-studio listview


【解决方案1】:

在你的 ListView xml 中添加这个:

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"

供参考:

【讨论】:

  • 请解释更多
【解决方案2】:

您的 ListView 没有受到适当的约束。

你可以试试这个方法:

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

    <ListView
        android:id="@+id/messages_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:divider="#fff"
        app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topBar" />


    <LinearLayout
        android:id="@+id/layout_chatbox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:minHeight="48dp"
        android:background="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">


        <EditText
            android:id="@+id/edittext_chatbox"
            android:hint="Enter message"
            android:background="@android:color/transparent"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:maxLines="6"
            />

        <Button
            android:id="@+id/button_chatbox_send"
            android:text="send"
            android:textSize="14dp"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="64dp"
            android:layout_height="48dp"
            android:gravity="center"
            android:textColor="@color/myColor"
            android:layout_gravity="bottom"
            android:onClick="sendMessage"/>

    </LinearLayout>
    <RelativeLayout
        android:id="@+id/topBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:minHeight="50dp"
        android:background="@color/white"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteY="0dp">

        <TextView
            android:id="@+id/name_of_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tomnaor"
            android:textColor="@color/myColor"
            android:textStyle="bold"
            android:textSize="20dp"
            android:layout_marginStart="30dp"
            android:layout_centerVertical="true"
            />

        <TextView
            android:id="@+id/emotion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Happy Chat"
            android:textColor="#939393"
            android:textSize="14dp"
            android:layout_toEndOf="@+id/name_of_user"
            android:layout_marginStart="4dp"
            android:layout_below="@+id/name_of_user"/>

    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 2021-05-26
    • 2015-04-17
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多