在ViewPager中使用视图或片段 [英] Use View or Fragment in ViewPager

查看:74
本文介绍了在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 ListViews 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最终是显而易见的选择.我有我的InputFormFragmentGraphFragment,它们每个都只包含它们提供的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, Fragments were the obvious choice in the end. I have my InputFormFragment and a GraphFragment, and they each contain only the applicable logic for the Views 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 Fragments, 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 ListViews within, you might find it takes more work in the future to support new kinds of Views or special tablet layouts.

我要说的一件事是,我自己通过FragmentPagerAdapterFragmentStatePagerAdapterViewPager中实现了Fragment,如果您有特殊要求,事情可能会有些尴尬.有时管理Fragment可能很棘手.例如,对于我的UI,我需要能够以编程方式添加和删除包含FragmentViewPager.我还需要确保使用中的适配器一旦显示后不会破坏Fragment,因为我需要在某个时刻同时从所有Fragment收集数据.此外,我必须扩展和修改FragmentPagerAdatper,以确保Fragment正确通过了它们的onDestroy(),并且在删除ViewPager时已从FragmentManager中删除.

One thing I will say is having implemented Fragments in a ViewPager myself through FragmentPagerAdapter and FragmentStatePagerAdapter, things can get a bit awkward if you have any special requirements; managing Fragments can be tricky sometimes. For example, for my UI I needed to be able to programmatically add and remove the ViewPager containing the Fragments. I also needed to ensure that the adapter in use didn't destroy Fragments once they had been shown, because I needed to collect data from all Fragments simultaneously at a certain point. Furthermore, I had to extend and modify FragmentPagerAdatper to make sure that the Fragments go through their onDestroy() properly and are removed from the FragmentManager when the ViewPager was removed.

Fragment启用了一种非常模块化的方式来构造用于各种屏幕尺寸和方向的UI,并且在它们如何允许您封装单个UI元素的业务逻辑和生命周期方面表现出色.但是,如果您的情况确实像ViewPager中的几个ListView一样简单,并且您知道您将永远不需要模块化,那么Fragment的开销可能就算过头了.

Fragments 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 ListViews in a ViewPager and you know that you will never need the modularity, then the overhead of Fragments could be an overkill.

这篇关于在ViewPager中使用视图或片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆