In my app I have a list of comments and a list with the Id of these comments. Following this answer I can scroll for example to the comment "-M7x0PJiuxJ6HcNjfQvr" after loading the data. But if I put my recyclerview inside a nestedscrollview it doesn't work anymore.
My layout:
<?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"
android:orientation="vertical"
tools:context=".activity.ComentariosActivity">
<include
android:id="@+id/toolbar_comentarios"
layout="@layout/toolbar_empty"
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_toTopOf="@+id/linearLayout5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_comentarios">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardBackgroundColor="@color/cardBackground"
app:layout_scrollFlags="scroll">
<!-- many views inside the card-->
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerComentarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/item_comentario" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My code:
private fun loadComments() {
comentariosRef = ConfiguracaoFirebase.comentariosRef().child(post.idPostagem!!)
valueEventListenerComentarios = comentariosRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
listaComentarios.clear()
listaIdComentarios.clear()
for (ds in dataSnapshot.children) {
listaComentarios.add(comentario)
listaIdComentarios.add(comentario.idComentario)
}
}
adapterComentarios.notifyDataSetChanged()
val index = listaIdComentarios.indexOf("-M7x0PJiuxJ6HcNjfQvr")
Handler().postDelayed({
recyclerComentarios.smoothScrollToPosition(index + 1)
}, 300)
}
override fun onCancelled(databaseError: DatabaseError) {
}
})
}
Why did this happen? How can I fix it?