【问题标题】:How to auto scroll downListView control when update new item- android studio更新新项目时如何自动向下滚动ListView控件-android studio
【发布时间】:2019-07-17 06:31:41
【问题描述】:

我的聊天页面有列表视图....每当我在列表视图中键入新消息时,它不会自动向下滚动到底部。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15sp"
    android:background="@drawable/chat_page"
    tools:context=".Chatbox">


    <Button
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:clickable="true"
        android:id="@+id/sendbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/send"
         />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/sendbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true">

        <EditText
            android:id="@+id/typemessage"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="Type a message..." />
    </android.support.design.widget.TextInputLayout>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_above="@id/sendbutton"
        android:dividerHeight="5dp"
        android:padding="10dp"
        android:divider="@android:color/transparent"
        android:id="@+id/listmessages"
        android:layout_marginBottom="16dp"/>


</RelativeLayout>

当我在列表视图中添加新文本时,它应该转到页面底部以查看新消息

【问题讨论】:

  • 添加每个项目后使用listview.smoothScrollToPosition(adapter.getCount());

标签: java android android-listview


【解决方案1】:

你可以试试这个。但我建议您使用 recyclerview 而不是聊天模块的 listview。

private void scrollToBottom() {
listView.post(new Runnable() {
    @Override
    public void run() {
        listView.setSelection(adapter.getCount() - 1);
    }
 });
}

【讨论】:

  • 酷,请接受这个作为答案,以便其他尝试相同的人容易。
  • Oki,您需要点击左侧的“箭头标记”才能接受此答案
猜你喜欢
  • 2011-01-02
  • 2019-06-27
  • 2021-11-07
  • 2019-06-14
  • 2018-12-16
  • 2021-10-29
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多