在ViewPager中使用视图或片段 [英] Use View or Fragment in ViewPager
问题描述
我有一个问题,是要在ViewPager中使用View还是Fragment.
I have a question about whether to use View or Fragment with ViewPager.
背景: 我有一个包含ListView的ActivityA.每个ListView项都会打开ActivityB.ActivityB显示不同的内容,具体取决于在Activity A中点击了哪个ListView项. 活动B的内容显示在ListView中.
Background: I have an Activity A that contains a ListView. Each ListView item opens Activity B. Activity B shows different content depending on which ListView item is tapped in Activity A. Activity B's content is shown inside a ListView.
问题: 现在,我不再需要在活动A和活动B之间来回切换内容,而是需要实现水平视图滑动以在活动B中全部切换内容. 我发现的一种解决方案(尝试并起作用)是创建Activity B的ListView的许多实例,并将其与ViewPager + PagerAdapter一起使用. 在文档上找到的另一种可能的解决方案(尚未尝试过)是将ListView放入一个Fragment中,创建该片段的许多实例,并将其与ViewPager + FragmentPagerAdapter或FragmentStatePagerAdapter一起使用.
Question: Now, instead of going back and forth between Activity A and B to switch contents, I have a requirement to implement horizontal view swiping to switch contents all within Activity B. One solution I found (tried it and it works) is to create many instances of Activity B's ListView and use it with ViewPager + PagerAdapter. Another potential solution found on the doc (haven't tried it) is to bring that ListView into a Fragment, create many instances of the fragment and use it with ViewPager + FragmentPagerAdapter or FragmentStatePagerAdapter.
我的问题是,使用每种方法的好处是什么?我应该解决将ListView放入Fragment的所有麻烦,还是仅将ListView与ViewPager一起使用?
My question is, what's the benefit of using each approach? Should I go through all the trouble of bringing the ListView into Fragment or just simply use ListView with ViewPager?
谢谢
推荐答案
Fragment
是一种有用的方法,当您想将某些UI业务逻辑绑定到特定的View
(或一组)时.如您所知,单个Fragment
具有其自己的生命周期回调等,就像Activity
一样.
A Fragment
is a useful approach, I think, when you want to tie some UI business logic to a particular View
(or group of). As you know, that individual Fragment
has its own lifecycle callbacks and so forth, just as an Activity
would.
与其通过单个PagerAdapter
拥有单个Activity
主机而不是多个ListView
,而是 使用Fragment
方法可能更干净,因为Fragment
只需要处理驱动单个ListView
背后的逻辑.
Rather than having a single Activity
host many ListView
s through a single PagerAdapter
, it may be cleaner to use the Fragment
approach because the Fragment
only needs to deal with the logic behind driving a single ListView
.
这与我刚刚面对的情况非常相似.我正在显示ViewPager
中的各种垂直滚动形式(由很多输入字段组成).就我而言,我选择了Fragment
方法,因为就我而言,ViewPager
实际上可能需要在某些页面上显示完全不同的视图.例如,在前几页上,可能会显示用户输入表单.但是在最后一页上将显示一个图形.需要一组完整的逻辑来驱动该图.从单个Activity
驱动这些输入形式和一个图形会有些混乱,而且我可能需要将业务逻辑包含在多个委托类中.因此,对我而言,Fragment
最终是显而易见的选择.我有我的InputFormFragment
和GraphFragment
,它们每个都只包含它们提供的View
的适用逻辑.
This is a very similar situation to one I've just been facing. I'm showing various vertically scrolling forms (consisting of lots of input fields) within a ViewPager
. In my case I have gone for the Fragment
approach because in my case, it's possible that the ViewPager
will actually need to display a completely different kind of view on certain pages. For example, on the first few pages, user input forms might be displayed. But on the final page, a graph will be displayed. A whole separate set of logic is required to drive that graph. To drive those input forms and one graph from a single Activity
would get a bit messy, and I would probably need to contain the business logic in several delegate classes or something. So for me, Fragment
s were the obvious choice in the end. I have my InputFormFragment
and a GraphFragment
, and they each contain only the applicable logic for the View
s that they supply.
要考虑的另一件事是,在不久的将来,您也可能希望在ViewPager
中显示另一种View
.或者,您可能希望完全拥有另一种UI布局,也许不使用ViewPager
而是将它们全部并排显示(例如,在横向模式下在大型平板电脑上使用的布局).使用Fragment
,事情就更加模块化了,您可以考虑使用代码来更快地完成此操作.另一方面,如果您通过使用一个包含简单PagerAdapter
和其中所有ListView
逻辑的单个Activity
实现了您的目标,则可能会发现将来需要更多的工作来支持新的View
或特殊的平板电脑布局.
Another thing to consider is that in the near future you too may want to display a different kind of View
in your ViewPager
. Or, you might want to have another UI layout altogether, perhaps one that doesn't use the ViewPager
but displays them all side-to-side (e.g. a layout used on a large tablet in landscape mode). With Fragment
s, things are just far more modular and you could factor the code to do this quicker. If on the other hand you achieved your objective by using a single Activity
that contains a simple PagerAdapter
and all the logic for the ListView
s within, you might find it takes more work in the future to support new kinds of View
s or special tablet layouts.
我要说的一件事是,我自己通过FragmentPagerAdapter
和FragmentStatePagerAdapter
在ViewPager
中实现了Fragment
,如果您有特殊要求,事情可能会有些尴尬.有时管理Fragment
可能很棘手.例如,对于我的UI,我需要能够以编程方式添加和删除包含Fragment
的ViewPager
.我还需要确保使用中的适配器一旦显示后不会破坏Fragment
,因为我需要在某个时刻同时从所有Fragment
收集数据.此外,我必须扩展和修改FragmentPagerAdatper
,以确保Fragment
正确通过了它们的onDestroy()
,并且在删除ViewPager
时已从FragmentManager
中删除.
One thing I will say is having implemented Fragment
s in a ViewPager
myself through FragmentPagerAdapter
and FragmentStatePagerAdapter
, things can get a bit awkward if you have any special requirements; managing Fragment
s can be tricky sometimes. For example, for my UI I needed to be able to programmatically add and remove the ViewPager
containing the Fragment
s. I also needed to ensure that the adapter in use didn't destroy Fragment
s once they had been shown, because I needed to collect data from all Fragment
s simultaneously at a certain point. Furthermore, I had to extend and modify FragmentPagerAdatper
to make sure that the Fragment
s go through their onDestroy()
properly and are removed from the FragmentManager
when the ViewPager
was removed.
Fragment
启用了一种非常模块化的方式来构造用于各种屏幕尺寸和方向的UI,并且在它们如何允许您封装单个UI元素的业务逻辑和生命周期方面表现出色.但是,如果您的情况确实像ViewPager
中的几个ListView
一样简单,并且您知道您将永远不需要模块化,那么Fragment
的开销可能就算过头了.
Fragment
s enable a very modular way of constructing UIs for various screen sizes and orientations, and are excellent in how they allow you to encapsulate business logic and lifecycles for individual UI elements. However if your scenario really is just as simple as several ListView
s in a ViewPager
and you know that you will never need the modularity, then the overhead of Fragment
s could be an overkill.
这篇关于在ViewPager中使用视图或片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!