android – Insert a RecyclerView inside another RecyclerView

This implementation is horizontal grid recycler view inside a
vertical Liner recycler view

Activity

class RecyclerGridActivity : AppCompatActivity() {

    private lateinit var listAdapter: RecyclerCustomListAdapter

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

        val recyclerListView = findViewById<RecyclerView>(R.id.recyclerCustomList)

        listAdapter = RecyclerCustomListAdapter()
        recyclerListView.layoutManager = LinearLayoutManager(this@RecyclerGridActivity)
        recyclerListView.adapter = listAdapter
        recyclerListView.addItemDecoration(TopSpacingItemDecoration(10))
        listAdapter.setContent(arrayOf(1,2,3,4), Example.shared.getData(), WeakReference(this))


    }
}

It’s layout

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


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

vertical Liner recycler Adapter

class RecyclerCustomListAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private lateinit var items: Array<Int>

    private lateinit var data: List<User>

    private lateinit var gridAdapter: RecyclerGridAdapter

    private lateinit var ctx: WeakReference<Context>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        gridAdapter = RecyclerGridAdapter()

        return ListViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.layout_custom_list_item, parent, false)
        )
    }

    fun setContent(items: Array<Int>, data: List<User>, ctx: WeakReference<Context>) {
        this.items = items
        this.data = data
        this.ctx = ctx
    }


    override fun getItemCount(): Int {
        return items.size
    }

    class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val recyclerGridView = itemView.findViewById<RecyclerView>(R.id.recyclerGrid)

        fun bind(data: List<User>, adapter: RecyclerGridAdapter, ctx: WeakReference<Context>) {

            recyclerGridView.layoutManager = GridLayoutManager( ctx.get(), 1, GridLayoutManager.HORIZONTAL, false)
            recyclerGridView.adapter = adapter
            recyclerGridView.addItemDecoration(TopSpacingItemDecoration(10))
            adapter.setContent(data)
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ListViewHolder-> {
                holder.bind(data, gridAdapter, ctx)
            }
        }
    }
}

It’s layout

<?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="200dp"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#4CAF50" />
</LinearLayout>

horizontal grid recycler adapter

class RecyclerGridAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private lateinit var items: List<User>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GridViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.layout_legacy_grid_item, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GridViewHolder -> {
                holder.bind(items[position])
            }
        }
    }

    fun setContent(items: List<User>) {
        this.items = items
    }


    override fun getItemCount(): Int {
        return items.size
    }

    class GridViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val emailLabel = itemView.findViewById<TextView>(R.id.legacyGridEmail)

        fun bind(user: User) {
            emailLabel.text = user.email
        }
    }

}

It’s layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="60dp"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/presence_away" />

        <TextView
            android:id="@+id/legacyGridEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:text="Email"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Data class

class Example private constructor() {

companion object {
    //single instance
    val shared: Example by lazy { Example() }
}

fun getData() : List<User> {
    return listOf(
        User("malithdulan7@gmail.com", "malith", "url"),
        User("malithdulan7@gmail.com", "malith", "url"),
        User("malithdulan7@gmail.com", "malith", "url"),
        User("malithdulan7@gmail.com", "malith", "url"),
        User("malithdulan7@gmail.com", "malith", "url")
    )
}

}

Leave a Comment