0

My code below doesnt work for each specific scrollto or smoothScrollTo, can anyone help me? I basically want to be able to dynamically scroll to a specific Item position in the activity, e.g. if a specific recyclerviewItem has been clicked on it will go to the activity and scroll to the specific item. The position variable has been ranging from "recyclerview.getChildAdapterPosition(itemview)" to itemview.Bottom which also doesn't work. Help would be appreciated.

     private fun focusOnView(scroll: ScrollView, itemView : View?,position :Int) {
        scroll.post(Runnable {
            if (editBox != null) {
                val x= 1

                // scroll.scrollTo(0,NestedScrollView.FOCUS_DOWN)
              //  scroll.smoothScrollTo(0, scroll.bottom)
                scroll.smoothScrollTo(0,position)
                //scroll.fullScroll(130)
            }
        })
    }



  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/contentreyclerpager"
                android:layout_width="match_parent"
                android:paddingTop="10dp"
                android:layout_height="wrap_content" />


        </RelativeLayout>


    </ScrollView>
MazMM
  • 3
  • 1
  • 1
    A few things here: 1. "scroll.smoothScrollTo(0,position)" takes coordinates in pixels, so I don't think passing a "position" there is what you want (assuming this is a RecyclerView adapter position). 2. RecyclerView has its own API for scrolling to items, which is probably what you want: "recyclerView.smoothScrollToPosition()" 3. You really shouldn't put a RecyclerView inside a ScrollView. Using a height of "wrap_content" for the RecyclerView means you aren't actually recycling, which defeats the point. 4. If you really need to nest them, you should use NestedScrollView instead. – Brian Yencho Jun 10 '22 at 16:01
  • 1
    I think ultimately what you want to do is to get rid of the ScrollView / RelativeLayout entirely and just call "recyclerView.smoothScrollToPosition(position)." – Brian Yencho Jun 10 '22 at 16:02

1 Answers1

0

Why you use the nestedScollView smoothScrollTo with the position you have to give x,y coordonate i think in your case you can use recycleView.smoothScrollToPosition(yourPosition);

Aymen Ben Salah
  • 489
  • 4
  • 13