当用户滚动时改变视图的位置

3
我有一个长屏幕,我在其中使用了NestedScrollView和RecyclerView,就像下面的屏幕一样:

enter image description here

如你所见,我在屏幕底部有一个名为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>

我使用带有工具栏的坐标布局。 - undefined
它可以在工具栏上运行,但我的问题与此无关。 - undefined
我明白了,我尝试了一个解决方案,但对我来说没有起作用。 - undefined
我在Chat GPT上也问过,但它没有帮助我。 - undefined
CoordinatorLayout不是让它始终显示,而是在滚动时显示并隐藏视图。我可以修复视图的隐藏问题,但我想让它始终显示在底部。 - undefined
显示剩余10条评论
1个回答

0
为了保持commentContainer在屏幕底部可见,并使其随滚动而移动,直到达到其正常位置,您可以对XML布局进行以下调整:
  1. commentContainer移出SwipeRefreshLayoutNestedScrollView之外。
  2. 使用RelativeLayout作为根布局,以简化定位。
  3. SwipeRefreshLayoutNestedScrollViewcommentContainer设置适当的约束。

修改后的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>

评论容器出了问题,它隐藏了视图,并且在我打开屏幕时没有出现在底部。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接