1

I have a nested scroll view, inside it there's an image view and a recycler view. I want recycler view to scroll all the way to the bottom whenever onResume() fragment method is called. Current Behavior is that it open the screen as the normal screen and didn't scroll at all.

I have tried various solutions but nothing is working.

This is the code for layout and Fragment class. Please help me. Nothing is happening.

<androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="true"
            android:focusableInTouchMode="true"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toTopOf="@+id/btnCall"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintVertical_chainStyle="spread_inside">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/constraintLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:descendantFocusability="blocksDescendants"
                app:layout_constraintBottom_toTopOf="@+id/btnYouGave"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                app:layout_constraintVertical_chainStyle="spread_inside">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/ivTransactionProfileImage"
                    android:layout_width="102dp"
                    android:layout_height="102dp"
                    android:layout_marginTop="16dp"
                    android:src="@drawable/ic_panda"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerViewTrnsaction"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    app:layout_constraintVertical_bias="0.0"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/ivTransactionProfileImage" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>





override fun onResume() {
    super.onResume()
    mainActivity.supportActionBar?.title = args.name
    recyclerViewTrnsaction.layoutManager = LinearLayoutManager(activity)

    launch {
        context?.let {
            val transactions = getAllTransactions(args.id)
            recyclerViewTrnsaction.adapter = TransactionAdapter(transactions)
            nestedScrollView.smoothScrollTo(0, nestedScrollView.height)
            recyclerViewTrnsaction.smoothScrollToPosition(recyclerViewTrnsaction.adapter!!.itemCount)
        }
    }
}

1 Answers1

1

Try scrolling the NestedScrollView instead of RecyclerView using this.

Replace the smooth scrolling code with

nestedScrollView.post {
   nestedScrollView.fullScroll(View.FOCUS_DOWN)
 }
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Hi Max, Thank you so much, finally this worked. One question, what does this "post" keyword do?, I'm new to Android. And is there any way we can reduce the scroll speed, i.e. increase the animation time? – Anmol Singh Sahi Jun 04 '20 at 04:57
  • please help me understand what does "post" keyword do? I'm quite new to android and programming. – Anmol Singh Sahi Jun 04 '20 at 15:11
  • @AnmolSinghSahi have a look [here](https://stackoverflow.com/questions/13840007/what-exactly-does-the-post-method-do) – Atiq Jun 04 '20 at 15:29