0

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

Padder
  • 105
  • 1
  • 1
  • 9
  • Does this answer your question? [IllegalStateException: TabLayoutMediator attached before ViewPager2 has an adapter](https://stackoverflow.com/questions/63130703/illegalstateexception-tablayoutmediator-attached-before-viewpager2-has-an-adapt) – Sam Chen Sep 07 '22 at 20:10
  • @SamChen Your suggestion seems not to solve my question. But thanks anyways. – Padder Sep 08 '22 at 19:23

1 Answers1

0

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.

  • Yes my recyclerview adapter is custom. Working the same way you said. But I can't see the similarity with the viewpager2. I do not have a list or a view to pass the list-items into the view. – Padder Sep 08 '22 at 08:33