0

I'm kinda new to Android App Development. Well, I'm playing around with RecyclerView. I have a parent recyclerview with a modal layout. Now the modal layout has a recyclerview (child recyclerview). I have managed to create adapters and scroll the list of main recyclerview. Unfortunately I don't find a way to scroll the child recyclerview. Here is the code that I'm playing around:

  1. I've already tried setting the adapter of child recyclerview in onBindViewHolder method of parent recyclerview adapter.

  2. Also, I tried setting the attributes nestedScrollingEnabled=true, descendantFocusability=blocksDescendants and focusableInTouchMode=true for child recyclerView.

Here's my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Parent recyclerview model (model.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Testing" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_marginVertical="8dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/modalRecyclerView"
        android:layout_width="wrap_content"
        android:layout_height="100dp" />
</LinearLayout>

Child recyclerview model (child_modal.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testing"/>
</LinearLayout>

In the MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MainActivity, RecyclerView.VERTICAL, false)
            adapter = ModalAdapter()
        }
    }

ModalAdapter:

class ModalAdapter : RecyclerView.Adapter<ModalAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context).inflate(R.layout.model, parent, false)
        return ViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return 5
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.view.modalRecyclerView.adapter = ChildModalAdapter()
        holder.view.modalRecyclerView.layoutManager = LinearLayoutManager(holder.view.context, RecyclerView.VERTICAL, false)
    }

    class ViewHolder(val view: View): RecyclerView.ViewHolder(view)
}

ChildModalAdapter:

class ChildModalAdapter : RecyclerView.Adapter<ChildModalAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context).inflate(R.layout.child_modal, parent, false)
        return ViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return 10
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    }

    class ViewHolder(val view: View): RecyclerView.ViewHolder(view)
}

Internal recyclerview doesn't scroll while the parent recyclerview scrolls fine. I'm trying to find a way to make the internal recyclerview to scroll along with parent recyclerview (I want both the recyclerviews to scroll).

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Karthik
  • 111
  • 1
  • 8
  • Do you want child RecyclerView and parent RecyclerView to scroll together (Child does not scroll but every item is showed), or both RecyclerViews to scroll separately (Both scroll but is not really user-friendly) ? – Mathieu Jul 09 '19 at 16:05
  • I want to achieve second one (Both RecyclerViews to scroll). – Karthik Jul 10 '19 at 03:30

1 Answers1

0

Okay, I fixed this by setting a listener.

Here's the code below that fixed it:

val mScrollChangeListener = object : RecyclerView.OnItemTouchListener {
        override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}

        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
            when (e.action) {
                MotionEvent.ACTION_MOVE -> {
                    rv.parent.requestDisallowInterceptTouchEvent(true)
                }
            }
            return false
        }

        override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
    }
    modalRecyclerView.addOnItemTouchListener(mScrollChangeListener)

I've added this code in the onBindViewHolder() of parent RecyclerView's adapter.

Karthik
  • 111
  • 1
  • 8