0

I had a view with a netstedScroll and a recyclerView that I was using to display my data so my fragment was like that:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:itemCount="10"
                tools:listitem="@layout/row_header" />
</androidx.core.widget.NestedScrollView>

The recyclerview had a custom adapter that I used is to get a recyclerview same as an expandable view (I didn't use the native one) and everything was great until my last update, so we decided to add more KPIs and informations on the recyclerview became at the very bottom of the view:

enter image description here

The problem now is when the user clicks on negociating, the list of products doesn't show automatically if he doesn't scroll manually, and feels like there isn't any changes. I want just on click of this part, to scroll, the problem is that I'm using a custom adapter:

private fun bindHeaderViewHolder(holder: RecyclerView.ViewHolder, position: Int, viewType: ViewType) {
        val dataIndex = viewType.dataIndex
        val headerViewHolder = holder as SectionHeaderViewHolder
        when(position){
           
            0 -> headerViewHolder.sectionTitle.text = "Sold out"
            1 -> headerViewHolder.sectionTitle.text = "Rent"
            2 -> headerViewHolder.sectionTitle.text = "On stock"
            3 -> headerViewHolder.sectionTitle.text = "Negociating"
        }
if (isExpanded(position)) {
            headerViewHolder.expandIcon.setImageResource(R.drawable.ic_expand_less_black_24dp)
            if(position == 3){
                Log.e("position delivered", "I'm here")
            }
        } else {
            headerViewHolder.expandIcon.setImageResource(R.drawable.ic_expand_more_black_24dp)
        }

Can someone help me or suggest a solution for that please?

6
  • 1
    Major point - never use recyclerview inside any scroll view or nested scroll view. Now about scrolling, you can use recyclerView.scrollToPosition(items.size() - 1) Commented May 17, 2021 at 11:24
  • @MohitAjwani I can't use it inside the adapter, already tried that Commented May 17, 2021 at 11:46
  • no you cannot use it inside the adapter, you need to perform it on recyclerView object. It can be done only from the fragment where you have the instance either using findViewById or kotlin synthetic or anything. You need to give callback to the fragment via a listener. Commented May 17, 2021 at 11:50
  • just added recyclerView.addOnItemClickListener(object: OnItemClickListener{ override fun onItemClicked(position: Int, view: View) { recyclerView.scrollToPosition(adapter.itemCount - 1 ) } }) the onclicklistener in the adapter doesn't work anymore, can't expand my items now Commented May 17, 2021 at 12:17
  • You cannot add item click on recyclerview. We need custom listener on header click. pass a listener reference to the adapter. on header viewholder - add a click listener. Inside that use your listener reference passed to the adapter and call the callback function. In the fragment where you are passing the listener, implement the function and add the scrollToPosition logic inside that. Commented May 17, 2021 at 12:39

1 Answer 1

0

You can connect a listener to the adapter and called it when needed indicating the clicked position within the adapter:

class YourAdapter(val changeItemListener: (Int) -> Unit) : RecyclerView.Adapter<YourViewHolder>() {
    ...
    changeItemListener(position)
    ...
}

After that you can from Activity or fragment create the adapter with the listener and within the listener you can call scrollToPosition or whatever you may need.

viewModelAdapter = YourAdapter(changeItemListener = {position -> your_recyclerview.smoothScrollToPosition(position)})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.