android recyclerview.scrollto or scrollto position not working in fragment

hello i have this layout with recycler view

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SuraDetails.SuraDetails">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorPrimary">
    <EditText
            android:id="@+id/search_edittext_details"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="false"
            android:background="@drawable/searchfieldback"
            android:layout_margin="7dp"
            android:padding="5dp"
    />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sura_details_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></androidx.recyclerview.widget.RecyclerView>

fragment

here is code of fragment

{
    adapter = SuraDetailsAdapter(sura.verses!!, context!!, sura)
    recyclerView!!.layoutManager=layoutManager
    recyclerView!!.adapter=adapter
    if (pos!=null){
        recyclerView!!.scrollToPosition(pos)
    }
    /*recyclerView!!.adapter=adapter
    recyclerView!!.layoutManager=layoutManager*/
    // recyclerView!!.scrollToPosition()
    return view
}

}

all works good

but! recyclerView!!.scrollToPosition()

not working , i read diffrenent questions related to this topic here on stack but nothing helps

EDIT

I reviewd my prev branch and discovored that there all works good but xml is more simple

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SuraDetails.SuraDetails">
<TextView
        android:id="@+id/sura_details_suraname_english"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
        android:id="@+id/sura_details_suraname_russian"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center"/>
<TextView
        android:id="@+id/sura_details_bismi_arabik"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center"/>
<TextView
        android:id="@+id/sura_details_bismi_ukr"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center"/>
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sura_details_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

maybe problem lays here

Update

i guess i found found source of problem my fragment is wrapped in nested scroll view so i have nestedscroll viewand in it recyclerview recycler view in nested scroll view

Upvotes: 0

Views: 2287

Answers (2)

Kasım &#214;zdemir
Kasım &#214;zdemir

Reputation: 5634

Can you try this :

recyclerView.post {
   recyclerView.scrollToPosition(pos)
}

or

recyclerView.postDelayed(Runnable {
   recyclerView.scrollToPosition(pos)
},2000)

Upvotes: 1

Chuong Le Van
Chuong Le Van

Reputation: 284

Did u try this:

recyclerView.smoothScrollToPosition(position)

or

(recyclerView.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(position, 0)

Hope this help!

Upvotes: 1

Related Questions