ViewPager2没有Fragments

huangapple go评论53阅读模式
英文:

ViewPager2 without Fragments

问题

我有一个应用程序,需要实现以下行为:我有一个XML布局,需要将其应用于每个ViewPager2页面,而不是使用片段。是否有这样的机会?

我有一个ArrayList,根据其大小,我需要在ViewPager2中创建相同数量的页面,并在每个页面中填充我的数组中的信息。页面可以有很多,这就是我不想为每个页面创建片段的原因。

英文:

I have an application and I need to realize a behavior where I have an XML layout that I need to apply for every ViewPager2 page NOT USING FRAGMENTS. Is there an opportunity?

I have an ArrayList and regarding his size I need to create the same amount of pages in ViewPager2 and fill every page with an info in my array. There can be plenty of pages, that's why I don't want to create a fragment for every page.

答案1

得分: 0

ViewPager2 本质上是 RecyclerView 的包装器,添加了布局和触摸处理,以适应具有页面大小(填充整个小部件大小)的项目。

setAdapter(RecyclerView.Adapter adapter) 允许您使用任何 RecyclerView 适配器。从来没有要求使用 FragmentStateAdapter

因此,只需实现一个普通的 RecyclerView 适配器,您的页面将使用视图持有者(使用您的 XML 布局创建)来显示。

英文:

ViewPager2 is essentially a RecyclerView wrapper that adds layout and touch handling to account for items that have size of a page (fill entire widgets size).

setAdapter(RecyclerView.Adapter adapter) lets you use any recycler view adapter. There was never a requirement to use a FragmentStateAdapter.

So just implement a normal recycler view adapter and your pages will be displayed using view holders (that are created using your XML layout).

huangapple
  • 本文由 发表于 2023年6月15日 02:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定