Nested RecyclerViews

In this guide, we'll be looking at how to implement a nested RecyclerView. We'll be having two RecyclerViews, one of it being a parent of a child RecylerView.

Although it is NOT encouraged to do this in practice, but it is good to know such complex layout can be done with it.

Source, please see at the bottom.

p/s: I haven't tried having many items loaded with this approach.

Layout structuring

Initial planning:


RecyclerView Parent (
	- Items as CardView
		- RecyclerView Child (
			- Items as Linear:Vertical listing with Custom LinearLayoutManager

Additional library


Because existing LinearLayoutManager in Support Library API doesn't support height WRAP_CONTENT, this library will help us doing that.


In ParentFragment, we're using standard LinearLayoutManager in Support Library. Pass both Parent and Child data to the Adapter.

LinearLayoutManager manager = new LinearLayoutManager(getActivity());

ParentAdapter parentAdapter = new ParentAdapter(getActivity(), getParentChildData());

In ParentAdapter, only thing we need to do is to have another RecyclerView as the item view of our Parent.

private void initChildLayoutManager(RecyclerView rv_child, ArrayList<Child> childData) { manager = new, LinearLayoutManager.VERTICAL, false);

    // disable touch gesture on our Child RV
    rv_child.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return true; }
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}

    ChildAdapter childAdapter = new ChildAdapter(childData); // pass child data to Child Adapter

By using a custom LinearLayoutManager, our Child view items won't get squeezed by the Parent's view item.

This is a known bug/issue in RecyclerView API and is now being made into next API release.

UPDATE: Support Library 23.2 release fixes this issue.

Source code


Show Comments

Get the latest posts delivered right to your inbox.