Android ViewPager2 with TabLayout with recyclerview (no fragments or views)

Viewed 58

hopefully I do not re-ask this question. If yes, please give me the other answer.

But now I go to my problem.

I am using an TabLayout with exactly 10 TabItems. Now I wish to swipe between them. Each TabItem do NOT contain a specific view or fragment. Underneath the tabItems is ONE recyclerView, whose elements changes depending on the active tabItem.

Connecting a viewPager2 with a tablayout is like following:

TabLayoutMediator(tablayout, viewpager) { tab, position -> }.attach()

But just that, gives me the error

TabLayoutMediator attached before ViewPager2 has an adapter

I have no idea, how that adapter has to look like. According to the docs, the adapter is nearly equal to a recyclerview.adapter .

May someone teach me the way?

Edit: I am stucking while creating an adapter WITHOUT an View. The function onCreateViewHolder returns a RecyclerView.ViewHolder(View) but I do not have a view. I just have one RecyclerView with an working Adapter which differs on the selected TabItem. The ViewPager2 just has to make it possible to swipe. Not more. Thats why I am overasked, why to have an adapter?

Help appreciated. Padder

1 Answers

Have you ever created recyclerview with custom adapter. There we have to set adapter to our recycler view each item is displayed in the list.

Similarly for view pager you also have to set adapter. Even if you have statically set the list of 10 tabs. Just pass it to the adapter or initialize it in the adapter constructor.

Here is the link for help.

Related