How to scroll all the way bottom of the nested scroll view?

31 Views Asked by At

I want to scroll all the way bottom of the nested scroll view. In my nested scroll view i am having a recycler view and below it also some view but it is scrolling only till the last item of recycler view.

Below is my XML code

<?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:background="@color/whiteSmoke"
tools:context=".activity.MessageActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmerFL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        app:shimmer_duration="800">

        <include layout="@layout/activity_message_shimmer" />

    </com.facebook.shimmer.ShimmerFrameLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/aBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/whiteSmoke"
        android:visibility="gone">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginHorizontal="@dimen/margin15">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/whiteSmoke"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <ImageView
                android:id="@+id/backIV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackground"
                android:clickable="true"
                android:contentDescription="@string/exit_back"
                android:focusable="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_back" />

            <TextView
                android:id="@+id/textType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/poppins_semi_bold"
                android:includeFontPadding="false"
                android:textAlignment="center"
                android:textColor="@color/gunmetal"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/toolbar"
                app:layout_constraintTop_toTopOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/aBarLayout"
        android:fillViewport="true"
        android:layout_marginVertical="@dimen/margin15"
        android:overScrollMode="ifContentScrolls"
        android:scrollbars="none"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <RelativeLayout
            android:id="@+id/nsv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvMessages"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin15"
                android:clipToPadding="false"
                android:scrollbars="none"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

            <LinearLayout
                android:id="@+id/lBottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rvMessages"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:padding="@dimen/margin15"
                android:visibility="gone">

                <ProgressBar
                    android:id="@+id/pbLoad"
                    android:layout_width="@dimen/margin30"
                    android:layout_height="@dimen/margin30"
                    android:layout_gravity="center"
                    android:visibility="gone" />

                <LinearLayout
                    android:id="@+id/llEditSend"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:visibility="gone">

                    <EditText
                        android:id="@+id/editTextQuestion"
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:background="@drawable/bg_edit_text"
                        android:fontFamily="@font/poppins_regular"
                        android:gravity="top|start"
                        android:hint="@string/type_question"
                        android:inputType="textMultiLine"
                        android:maxLines="5"
                        android:minLines="3"
                        android:paddingHorizontal="@dimen/margin15"
                        android:paddingVertical="12dp"
                        android:textColorHint="@color/silverChalice"
                        android:textSize="14sp" />

                    <LinearLayout
                        android:id="@+id/llQuestionError"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp"
                        android:gravity="center"
                        android:orientation="horizontal"
                        android:visibility="gone">

                        <ImageView
                            android:layout_width="0dp"
                            android:layout_height="@dimen/margin20"
                            android:layout_weight="0.05"
                            android:src="@drawable/ic_error_info" />

                        <TextView
                            android:id="@+id/textErrorQuestion"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:fontFamily="@font/poppins_regular"
                            android:paddingHorizontal="@dimen/paddingBottom5"
                            android:text="@string/question_required"
                            android:textColor="@color/red"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/ivSend"
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="@dimen/margin15"
                        android:background="@drawable/bg_btn"
                        android:fontFamily="@font/poppins_medium"
                        android:paddingHorizontal="28dp"
                        android:paddingVertical="14dp"
                        android:text="@string/upload"
                        android:textAllCaps="false"
                        android:textColor="@color/white"
                        android:textSize="14sp" />

                </LinearLayout>

                <RelativeLayout
                    android:id="@+id/btnContinue"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_btn"
                    android:clickable="true"
                    android:focusable="true"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginVertical="@dimen/margin15"
                        android:fontFamily="@font/poppins_medium"
                        android:includeFontPadding="false"
                        android:text="@string/continue_conversation"
                        android:textColor="@color/white"
                        android:textSize="14sp" />

                </RelativeLayout>


                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/btnSendFeedback"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:layout_marginVertical="@dimen/margin15"
                    android:background="@drawable/bg_btn_faq"
                    android:fontFamily="@font/poppins_medium"
                    android:text="@string/send_feedback"
                    android:textAllCaps="false"
                    android:textSize="14sp" />

            </LinearLayout>

        </RelativeLayout>

    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

Below is code that i am trying

private fun getMessages(threadId: String) {
    binding.shimmerFL.visibility = View.VISIBLE
    val getMessagesCall: Call<GetMessagesResponse>? = RestClient.client?.getMessages(threadId)

    getMessagesCall?.enqueue(object : Callback<GetMessagesResponse?> {
        override fun onResponse(
            call: Call<GetMessagesResponse?>, response: Response<GetMessagesResponse?>
        ) {
            if (response.isSuccessful) {
                getMessagesResponse = response.body()!!
                if (getMessagesResponse.status == "success") {
                    binding.shimmerFL.visibility = View.GONE
                    binding.aBarLayout.visibility = View.VISIBLE
                    binding.nsv.visibility = View.VISIBLE
                    binding.lBottom.visibility = View.VISIBLE

                    // Initialize and set adapter
                    messageList = getMessagesResponse.data?.messages!!

                    adapter = MessageAdapter(messageList)
                    binding.rvMessages.adapter = adapter

                    // Wait for the RecyclerView to be laid out before scrolling
                    binding.rvMessages.viewTreeObserver.addOnGlobalLayoutListener(object :
                        ViewTreeObserver.OnGlobalLayoutListener {
                        override fun onGlobalLayout() {
                            // Remove the listener to avoid multiple calls
                            binding.rvMessages.viewTreeObserver.removeOnGlobalLayoutListener(
                                this
                            )
                            // Scroll to the bottom of the NestedScrollView
                            binding.scrollView.post {
                                binding.rvMessages.scrollToPosition(adapter.itemCount - 1)
                                binding.scrollView.fullScroll(View.FOCUS_DOWN)
                            }
                        }
                    })


                } else {

                    val snackBar = Snackbar.make(
                        binding.root, "", Snackbar.LENGTH_LONG
                    )
                    val customSnackView: View =
                        layoutInflater.inflate(R.layout.custom_snackbar_layout, null)
                    snackBar.view.setBackgroundColor(Color.TRANSPARENT)
                    val snackbarLayout = snackBar.view as Snackbar.SnackbarLayout
                    snackbarLayout.setPadding(0, 0, 0, 0)
                    val textSnack: TextView = customSnackView.findViewById(R.id.textSnack)
                    textSnack.text = resources.getString(R.string.wentWrong)
                    val ivClose: ImageView = customSnackView.findViewById(R.id.ivClose)
                    ivClose.setOnClickListener { snackBar.dismiss() }
                    snackbarLayout.addView(customSnackView, 0)
                    snackBar.show()
                }
            } else {
                binding.shimmerFL.visibility = View.GONE
                binding.llNoConnection.visibility = View.VISIBLE
                val snackBar = Snackbar.make(
                    binding.root, "", Snackbar.LENGTH_LONG
                )
                val customSnackView: View =
                    layoutInflater.inflate(R.layout.custom_snackbar_layout, null)
                snackBar.view.setBackgroundColor(Color.TRANSPARENT)
                val snackbarLayout = snackBar.view as Snackbar.SnackbarLayout
                snackbarLayout.setPadding(0, 0, 0, 0)
                val textSnack: TextView = customSnackView.findViewById(R.id.textSnack)
                textSnack.text = resources.getString(R.string.wentWrong)
                val ivClose: ImageView = customSnackView.findViewById(R.id.ivClose)
                ivClose.setOnClickListener { snackBar.dismiss() }
                snackbarLayout.addView(customSnackView, 0)
                snackBar.show()

                val errorBody = response.errorBody()?.string()

                try {
                    val errorResponse = Gson().fromJson(errorBody, ErrorResponse::class.java)

                    val snackBar = Snackbar.make(
                        binding.root, "", Snackbar.LENGTH_LONG
                    )
                    val customSnackView: View =
                        layoutInflater.inflate(R.layout.custom_snackbar_layout, null)
                    snackBar.view.setBackgroundColor(Color.TRANSPARENT)
                    val snackbarLayout = snackBar.view as Snackbar.SnackbarLayout
                    snackbarLayout.setPadding(0, 0, 0, 0)
                    val textSnack: TextView = customSnackView.findViewById(R.id.textSnack)
                    textSnack.text = errorResponse.errorMessage
                    val ivClose: ImageView = customSnackView.findViewById(R.id.ivClose)
                    ivClose.setOnClickListener { snackBar.dismiss() }
                    snackbarLayout.addView(customSnackView, 0)
                    snackBar.show()

                } catch (e: Exception) {
                    // Handle parsing error
                }

            }
        }

        override fun onFailure(call: Call<GetMessagesResponse?>, t: Throwable) {
            binding.shimmerFL.visibility = View.GONE
            binding.llNoConnection.visibility = View.VISIBLE

            val snackBar = Snackbar.make(
                binding.root, "", Snackbar.LENGTH_LONG
            )
            val customSnackView: View =
                layoutInflater.inflate(R.layout.custom_snackbar_layout, null)
            snackBar.view.setBackgroundColor(Color.TRANSPARENT)
            val snackbarLayout = snackBar.view as Snackbar.SnackbarLayout
            snackbarLayout.setPadding(0, 0, 0, 0)
            val textSnack: TextView = customSnackView.findViewById(R.id.textSnack)
            textSnack.text = resources.getString(R.string.wentWrong)
            val ivClose: ImageView = customSnackView.findViewById(R.id.ivClose)
            ivClose.setOnClickListener { snackBar.dismiss() }
            snackbarLayout.addView(customSnackView, 0)
            snackBar.show()
        }

    })
}

I am trying , but still it only scrolls to recyclerview

0

There are 0 best solutions below