在用户滚动时更改视图的位置

问题描述 投票:0回答:1

我有一个长屏幕,我使用

NestedScrollView
RecyclerView
来搭配它,就像下面的屏幕一样:

如您所见,我在屏幕底部有一个

commentConatiner
布局,当前用户在滚动到底部时会看到它。

但是,我需要用户在打开屏幕底部的屏幕时看到

commentConatiner
;当用户滚动时,
commentConatiner
视图应随滚动移动,直到
ScrollView
到达
commentConatiner
位置。

我的意思是它应该一直显示(在底部),并且当用户滚动屏幕时,它应该随着滚动移动,直到到达正常位置。

这是我的代码:

<?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">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="29dp"
        android:layout_marginEnd="29dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toTopOf="@+id/commentConatiner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="26dp">

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

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rvChat"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:itemCount="1"
                    tools:listitem="@layout/item_discussion" />


                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/noDataContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="16dp"
                    android:visibility="gone"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <ImageView
                        android:id="@+id/imageView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_comma_green"
                        app:layout_constraintBottom_toTopOf="@+id/tvNoData"
                        app:layout_constraintEnd_toStartOf="@+id/tvNoData" />

                    <TextView
                        android:id="@+id/tvNoData"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/poppins_regular"
                        android:text="@string/there_is_no_discussion"
                        android:textColor="@color/kafd_color_blue"
                        android:textSize="@dimen/x13sp"
                        android:textStyle="normal"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/commentConatiner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shadow_white_bg"
        android:elevation="10dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <EditText
            android:id="@+id/etComment"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginStart="33dp"
            android:layout_marginTop="@dimen/x8dp"
            android:layout_marginEnd="10dp"
            android:background="@android:color/transparent"
            android:ems="10"
            android:fontFamily="@font/poppins_regular"
            android:gravity="start|center"
            android:hint="@string/write_here"
            android:inputType="textMultiLine"
            android:isScrollContainer="true"
            android:lineSpacingExtra="3sp"
            android:scrollbars="vertical"
            android:textColor="@color/kafd_color_gray_1"
            android:textColorHint="@color/kafd_color_gray_1"
            android:textSize="@dimen/x14sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@+id/rvSelectedFiles"
            app:layout_constraintEnd_toStartOf="@+id/ivFile"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/ivSend"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_send"
            app:layout_constraintBottom_toTopOf="@+id/rvSelectedFiles"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/ivFile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="15dp"
            android:padding="5dp"
            android:src="@drawable/ic_add_attachment"
            app:layout_constraintBottom_toBottomOf="@+id/ivSend"
            app:layout_constraintEnd_toStartOf="@+id/ivSend"
            app:layout_constraintTop_toTopOf="@+id/ivSend" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvSelectedFiles"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginBottom="15dp"
            android:visibility="gone"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/ivSend"
            app:layout_constraintStart_toStartOf="@+id/etComment"
            app:spanCount="2"
            tools:itemCount="4"
            tools:listitem="@layout/item_selected_attachment" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin android-animation android-constraintlayout android-scrollview
1个回答
0
投票

要保持

commentContainer
在屏幕底部可见并使其随滚动条移动直至到达正常位置,您可以对 XML 布局进行以下调整:

  1. commentContainer
    移到
    SwipeRefreshLayout
    NestedScrollView
    之外。
  2. 使用
    RelativeLayout
    作为根布局以简化定位。
  3. SwipeRefreshLayout
    NestedScrollView
    commentContainer
    设置适当的约束。

修改后的 XML 布局示例:

<?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">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- Your existing NestedScrollView content here -->

        </androidx.core.widget.NestedScrollView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/commentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shadow_white_bg"
        android:elevation="10dp"
        android:visibility="visible"
        app:layout_alignParentBottom="true">

        <!-- Your existing commentContainer content here -->

    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.