Changing the position of a view while user scrolling

136 Views Asked by At

I have a long screen and I use NestedScrollView and a RecyclerView with it like the below screen:

enter image description here

As you see I have a commentConatiner layout at the bottom of the screen, currently the user sees it when they scroll to the bottom.

But, I need the user to see the commentConatiner when they open the screen at the bottom of the screen; and when the user scrolls, the commentConatiner view should move with the scroll until the ScrollView reaches to the commentConatiner position.

I mean it should be shown all the times (at the bottom) and when user scroll the screen, it should move with scroll until it reaches to its normal position.

Here is my code:

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

There are 1 best solutions below

1
diziaq On

To keep the commentContainer visible at the bottom of the screen and having it move with the scroll until it reaches its normal position, you can make the following adjustments to your XML layout:

  1. Move the commentContainer outside the SwipeRefreshLayout and the NestedScrollView.
  2. Use a RelativeLayout as the root layout to simplify positioning.
  3. Set appropriate constraints for the SwipeRefreshLayout, NestedScrollView, and the commentContainer.

Example of modified XML layout:

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