android – how to add RecyclerView in one of the inflated layout of viewpager2

Exception or error:
  • I’m trying to create a RecyclerView in each layout of viewpager2

  • for viewpager2 I’m using RecyclerView adapter to inflate layouts

1. main layout

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/id_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_settings_package_tab_layout" />

2. I’m calling an adapter

id_view_pager.adapter = MultiViewAdapter(this, list)

3. MultiViewAdapter

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            R.layout.fragment_pkg_set_type -> {
                var view: View = layoutInflater.inflate(R.layout.fragment_layout, parent, false)
                Log.d("log", "onCreateViewHolder : Layout inflating is called")
                return ViewHolder1(view)
            }
            else -> {
                var view: View =
                    layoutInflater.inflate(R.layout.fragment_layout_others, parent, false)
                return ViewHolder2(view)
            }
        }
    }

4. fragment_layout

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_pkg_set_type_list_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/fragment_pkg_type_list_ui"/>

    <EditText
        android:id="@+id/id_pkg_type_value"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="Add Type"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/id_pkg_type_add"
        app:layout_constraintStart_toStartOf="parent" />
  • now I’m getting the all the inflated layouts and EditText was visible
  • now in order to load RecyclerView I wrote one more file corresponds to fragment_layout file

5. LayoutFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        var myDataset = arrayOf("one", "two", "three", "four", "five")
        var viewManager = LinearLayoutManager(context)
        var viewAdapter = PackageTypeAdapter(context, myDataset)

        viewManager.orientation = LinearLayoutManager.VERTICAL
        id_pkg_set_type_list_rv.setHasFixedSize(true)
        id_pkg_set_type_list_rv.layoutManager = viewManager
        id_pkg_set_type_list_rv.adapter = viewAdapter

        viewAdapter.notifyDataSetChanged()
    }

    class PackageTypeAdapter(context: Context?, data: Array<String>) :
        RecyclerView.Adapter<PackageTypeAdapter.MyViewHolder>() {

        var mData: Array<String>

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): PackageTypeAdapter.MyViewHolder {
            var layoutInflater = LayoutInflater.from(parent.context)
            var view = layoutInflater.inflate(R.layout.fragment_pkg_type_list_ui, parent, false)
            return MyViewHolder(view)
        }

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

        override fun onBindViewHolder(holder: PackageTypeAdapter.MyViewHolder, position: Int) {
            holder.text_value.text = mData[position]
        }

        inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var text_value: TextView

            init {
                text_value = itemView.findViewById(R.id.id_text_value)
            }
        }

        init {
            mData = data
        }

    }
  • unfortunately it is not reading this file
  • So, I’m not getting RecyclerView list

How to load nested RecyclerView to Viewpager2 which uses a RecyclerView to inflate layouts into viewpager2 ?

How to solve:

I think you confused a lot. and That’s why you are creating LayoutFragment.kt

You have to delete that file and add

is ViewHolder1 -> {
                var myDataset = arrayOf("one", "two", "three", "four", "five")
                var viewManager = LinearLayoutManager(ctx)
                var viewAdapter = PkgSetTypeFragment.PackageTypeAdapter(ctx, myDataset)

                viewManager.orientation = LinearLayoutManager.VERTICAL
                holder.id_pkg_set_type_list_rv.setHasFixedSize(true)
                holder.id_pkg_set_type_list_rv.layoutManager = viewManager
                holder.id_pkg_set_type_list_rv.adapter = viewAdapter

                viewAdapter.notifyDataSetChanged()

                holder.imageButton.setOnClickListener(View.OnClickListener {

                })
            }
            is ViewHolder2 -> holder.myTextView.text = mData[position]

inside on view bind holder file.

try to read basics

Leave a Reply

Your email address will not be published. Required fields are marked *