CodeWithVikas
CodeWithVikas

Reputation: 1443

Why can we not attach same LayoutManager Object with multiple RecyclerView in Android?

Getting following error:

java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager@f8f0a is already attached to a RecyclerView: android.support.v7.widget.RecyclerView

following is the code(Kotlin Code Snip)

val layoutmanager = LinearLayoutManager(context)
    layoutmanager.orientation = LinearLayoutManager.HORIZONTAL

    val parentView = view!!.findViewById<View>(R.id.solid_fill_picker)
    val recyclerView1 = parentView.findViewById<RecyclerView>(R.id.theme_color_palette1)

    recyclerView1.layoutManager = layoutmanager
val layoutmanager = LinearLayoutManager(context)
    layoutmanager.orientation = LinearLayoutManager.HORIZONTAL


    val recyclerView2 = parentView.findViewById<RecyclerView>(R.id.theme_color_palette1)

    recyclerView2.layoutManager = layoutmanager

Upvotes: 1

Views: 1434

Answers (1)

Wackaloon
Wackaloon

Reputation: 2365

That's because LayoutManager keeps the reference to RecyclerView inside of itself, and when you try to use the same LayoutManager again - it gives you that error. One LayoutManager can manage only one instance of RecyclerView. If you want to use LayoutManager with the same params and not create another one from scratch - you can create LayoutManager factory and get each time new LyaoutManger with the same params.

Upvotes: 2

Related Questions