Android.app Fragments 与 android.support.v4.app 使用 ViewPager?

Android.app Fragments vs. android.support.v4.app using ViewPager?(Android.app Fragments 与 android.support.v4.app 使用 ViewPager?)

本文介绍了Android.app Fragments 与 android.support.v4.app 使用 ViewPager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Android 支持库、片段以及作为具体示例的 ViewPager 类的问题.我的目的是创建一个与 Android 开发者网站 (http://developer.android.com/training/animation/screen-slide.html 或 http://developer.android.com/training/implementing-navigation/lateral.html).查看他们的代码,我注意到他们使用了 android.support.v4.app 库,根据我的研究,这是访问 ViewPager 类的唯一方法.

I have a question regarding the Android Support Libraries, Fragments, and as a specific example, the ViewPager class. My intention is to create an app with similar functionality to the sample provided on the Android Developer website (http://developer.android.com/training/animation/screen-slide.html or http://developer.android.com/training/implementing-navigation/lateral.html). Looking into their code, I've noticed they utilize the android.support.v4.app library, which from my research is the only way to access the ViewPager class.

在我的情况下,我对向后兼容性没有兴趣.最低 API 级别为 14(冰淇淋三明治),构建目标为 4.2 Jelly Bean.在最简单的形式中,我的应用程序的性能与我在 Android 开发网站上链接的第二个演示完全相同 - 只需在三个选项卡之间滑动,每个选项卡都有内容.

In my situation, I have no interest in backward compatibility. The minimum API level is 14 (Ice Cream Sandwich) and the build target is 4.2 Jelly Bean. In it's simplest form, my app performs exactly as does the second demo I linked on the Android dev website - just swiping between three tabs with content in each.

我读过的所有文章/帖子/答案似乎都非常支持 v4 支持库.现在我的问题虽然冗长,但:

All of the articles/posts/answers I've read seem to heavily favor the v4 support library. Now for my, albeit long-winded, question(s):

  1. 构建我的应用程序的最佳方式是什么 - 使用 android.support.v4.app,从而使用 SupportFragments,或者使用 android.app - 为什么?

  1. What's the best way to structure my application - using android.support.v4.app, and thereby using SupportFragments, or to use the Fragments provided in android.app - and why?

如果来自 android.app 的片段是可行的方法,那么接近 ViewPagers 的最佳方式是什么?

If Fragments from android.app are the way to go, what is the optimal way to approach ViewPagers?

如果 SupportFragments 最适合该任务,我估计它们具有与另一个相同的功能 - 那么将它们放在 android.app 中的目的是什么?

If SupportFragments are best-suited to the task, I would estimate that they possess the same functionality as the other - so what's the purpose of having them at all inside android.app?

希望有更清楚理解的人可以给我一点澄清,因为我很困惑......

Hopefully someone with a clearer understanding can give me a bit of clarification because I'm boggled...

推荐答案

您可以将 ViewPagerandroid.app 包中的原生片段与 android.app 包中的适配器一起使用一个 href="http://developer.android.com/reference/android/support/v13/app/package-summary.html" rel="noreferrer">android.support.v13.app 包.为此,您必须使用 v13 支持 jar.

You can use ViewPager with native fragments from the android.app package with the adapters from the android.support.v13.app package. You have to use the v13 support jar for that.

有两个版本的适配器可以与 ViewPager 一起使用,v4 包中的那些用于支持片段, 中的那些v13 带有原生片段.

There are two versions of the adapters that work with ViewPager, the ones in the v4 package are meant to be used with support fragments, the ones in v13 with native fragments.

现在有两个 Fragment 实现的原因是历史性的:android.app 包中的 Fragment 仅在 Android 3 中针对平板电脑引入,并且创建了支持库以将 Fragment 带到运行较旧的手机上版本.在 Android 4 上,两者都有.

The reason why there are now two fragment implementations is historical: Fragments in the android.app package were introduced with Android 3 for tablets only and the support library was created to bring fragments to phones running older versions. On Android 4 you have both.

根据我自己的经验,我建议使用支持片段,即使是在为 Android 4 开发时也是如此.以下是一些原因:片段或支持片段?

From my own experience I would recommend using support fragments, even when developing for Android 4. Here are some reasons: Fragment or Support Fragment?

这篇关于Android.app Fragments 与 android.support.v4.app 使用 ViewPager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android.app Fragments 与 android.support.v4.app 使用 ViewPager?

基础教程推荐