在显示键盘时以编程方式滚动滚动滚动视图

of1yzvn4  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(318)

我已经搜索了很长时间,但没有一个答案是有用的。
我正在制作一个聊天应用程序。当用户单击输入编辑文本时,键盘会显示出来,我想自动滚动到滚动视图的底部,但它无法按预期工作。它似乎认为scrollview的维度与实际不同(可能没有意识到键盘占用了一些空间)
当键盘被隐藏时,按程序滚动到底部的工作方式与预期相同。
这就是我现在拥有的:
在舱单中:

<activity
    android:name=".ChatActivity"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait" />

在活动中:

messageEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (!hasFocus)
                {
                    HideKeyboard(v);
                   // ScrollToBottom ();
                }
                else
                {
                    ScrollToBottom();
                    messagesScrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            messageEditText.requestFocus();
                        }
                    }); 

                    ScrollToBottom();
                }
            }
        });

  private void ScrollToBottom ()
    {
        messagesLinearLayout.requestLayout();
        messagesLinearLayout.post(new Runnable() {
            @Override
            public void run() {
                messagesScrollView.scrollTo(0, messagesScrollView.getBottom());
            }
        });
    }

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/black"
    android:clickable="true"
    android:focusableInTouchMode="true"
    tools:context=".ChatActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.LetsGOFriends.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="66dp"
            android:background="#242A2F"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:contentInsetEnd="0dp"
            app:buttonGravity="center_vertical"
            app:popupTheme="@style/Theme.LetsGOFriends.PopupOverlay">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical|start">

                <TextView
                    android:id="@+id/trainerNameChat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/profilePictureImageView"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="-2dp"
                    android:layout_toEndOf="@+id/profilePictureImageView"
                    android:maxLines="1"
                    android:textSize="14sp"
                    android:text="LoriMontana"
                    android:textColor="@color/white" />

                <TextView
                    android:id="@+id/trainerCodeChat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/profilePictureImageView"
                    android:layout_alignStart="@+id/trainerNameChat"
                    android:layout_marginBottom="-2dp"
                    android:layout_marginStart="0dp"
                    android:maxLines="1"
                    android:textSize="14sp"
                    android:text="0467 0321 3849"
                    android:textColor="@color/white" />

                <com.google.android.material.imageview.ShapeableImageView
                    android:id="@+id/profilePictureImageView"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="15dp"
                    android:layout_marginStart="0dp"
                    android:layout_marginTop="15dp"
                    android:padding="1dp"
                    app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
                    app:srcCompat="@drawable/ic_default_profile_picture"
                    app:strokeColor="#484848"
                    app:strokeWidth="1dp" />

                <ImageView
                    android:id="@+id/languageFlagChatImageView"
                    android:layout_width="18dp"
                    android:layout_height="16dp"
                    android:layout_alignBottom="@+id/trainerNameChat"
                    android:layout_alignTop="@+id/trainerNameChat"
                    android:layout_marginBottom="4dp"
                    android:layout_marginStart="5dp"
                    android:layout_marginTop="4dp"
                    android:layout_toEndOf="@+id/trainerNameChat"
                    tools:srcCompat="@tools:sample/avatars" />
            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <ScrollView
        android:id="@+id/messagesScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="-15dp"
        android:focusableInTouchMode="true"
        app:layout_constraintBottom_toTopOf="@+id/cardView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout">

        <LinearLayout
            android:id="@+id/messagesLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusableInTouchMode="true"
            android:paddingBottom="20dp"
            android:paddingTop="5dp"
            android:orientation="vertical"
            android:focusable="true" />
    </ScrollView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:cardCornerRadius="0dp"
        app:cardElevation="0dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout2"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="5dp"

                app:boxBackgroundColor="#242A2F"
                app:boxCornerRadiusBottomEnd="15dp"
                app:boxCornerRadiusBottomStart="15dp"
                app:boxCornerRadiusTopEnd="15dp"
                app:boxCornerRadiusTopStart="15dp"
                app:boxStrokeColor="@color/teal_700"
                app:boxStrokeWidth="0dp"
                app:boxStrokeWidthFocused="0dp"
                app:endIconContentDescription="Send"
                app:endIconDrawable="@drawable/ic_baseline_send_24"
                app:endIconMode="custom"
                app:endIconTint="@color/white"
                app:hintEnabled="false"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="Enter your message.."
                    android:maxLength="250"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textColor="@color/white"
                    android:textColorHint="#D3D3D3" />

            </com.google.android.material.textfield.TextInputLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

我认为布局文件有问题。

gcuhipw9

gcuhipw91#

请尝试使用以下代码向下滚动:

messagesScrollView.fullScroll(ScrollView.FOCUS_DOWN);

相关问题