40

I have searched a bit around for a pager for fragment , I develop on ICS API.

I know about ViewPager and all the support libs for earlier version but i don't get why I should use a support library consider the fact that I use the lastest version of the api and don't plan to support earlier version.

Do I have to write the "ViewPager" myself or is there something i didn't see in the api.

eephyne
  • 911
  • 9
  • 15

1 Answers1

49

Does the Android ICS API have a native equivalent to ViewPager support lib?

No.

I know about ViewPager and all the support libs for earlier version but i don't get why I should use a support library consider the fact that I use the lastest version of the api and don't plan to support earlier version.

The Android Support package is not only for backports of newer APIs. It is also for other classes that, for whatever reason, are not being added to the SDK, such as ViewPager and its supporting classes.

Do I have to write the "ViewPager" myself or is there something i didn't see in the api.

You are welcome to write your own implementation of a view paging component. Savvy programmers would use the one in the Android Support package, since it is already written and (mostly) debugged.

UPDATE: Note that ViewPager works just fine with pages that are:

  • API Level 11+ native fragments
  • Android Support backported fragments
  • arbitrary Views

For the first case, you need the v13 version of the support JAR, which contains v13 versions of the FragmentPagerAdapter and FragmentStatePagerAdapter classes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • that's exactly the ind of answer i was looking for , thanks. Now i need to get why i always got a ClassNotFoundException when using support libs. – eephyne Apr 01 '12 at 09:15
  • @eephyne: If you are on the R17 version of the tools, make sure your JARs are in a `libs/` directory in your project. The build tools will take care of adding it to your build path (Eclipse or command-line) and will package the JARs in your APK. – CommonsWare Apr 01 '12 at 10:41
  • i tested it both ways , adding them with eclipse manually (java build path > add external jars) and by creating libs directory and put the jar in it , same result. It's always when i try to access any of the class in support lib (either my app or support api demos). i tried like some says to put the support libs in first place in the "order and export" tab in java build path . – eephyne Apr 01 '12 at 11:13
  • 1
    @eephyne: Delete the copies you have, right click over the project, and choose Android Tools > Add Support Library, and see if that works better. – CommonsWare Apr 01 '12 at 11:28
  • 4
    This is kind of unfortunate, because the ActionBar.TabListener that you use with the ActionBar Tabs requires the app.Fragment, it doesn't work with the support Fragment. This makes it more complicated to use both a ViewPager and ActionBar Tabs in one app. – Christine Jul 30 '12 at 15:13
  • @Christine: Action bar tabs most certainly work with fragments from the Android Support package. You just ignore the `FragmentTransaction` that is passed to your `TabListener` and create your own `FragmentTransaction` from the Support package, or update your UI without fragment transactions. – CommonsWare Jul 30 '12 at 15:30
  • Mark, that's more or less what I did. Thanks anyway, for the reply. I now have an app that uses the actionbar tabs and a view pager, and roboguice. A remaining issue is that the support package doesn't support the tabs. I'm working on that one. – Christine Aug 01 '12 at 22:34
  • 8
    i don't understand why they wouldn't add viewpager to the main sdk if its such an important widget? if you are not targeting ics and below (e.g tablet only app) then is there a reason to use the support library? – numan salati Dec 20 '12 at 01:10
  • +1 this is annoying tho. -- I especially since I already wrote it to use the new API, and action bar, now if I want to implement scrolling, I either need to write my own scroller (not a big deal, but probably wont bother) or convert everything to the support API library. and then I cringe to think in a future API they will include a view pager for the real fragments, and will have to un-jump through all the hoops! – 101chris Apr 25 '13 at 17:57
  • 9
    @101chris: `ViewPager` works with either the Android Support package's fragments, native API Level 11 fragments, or your own views. "in a future API they will include a view pager for the real fragments" -- it already exists. Use the `android-support-v13.jar` for `ViewPager`, plus `FragmentPagerAdapter` and `FragmentStatePagerAdapter` for native API Level 11 fragments. – CommonsWare Apr 25 '13 at 18:17
  • @CommonsWare: woah. -- I think you saved me some frustration. Wish that was more obvious to me. The eclipse wizard puts in v4, even though the min sdk is 13. I don't mind using a support library, just didn't want to convert everything to the backwards-compatible classes. – 101chris Apr 25 '13 at 19:34
  • @CommonsWare android-support-v13.jar does not seem to contain the ViewPager. What am I missing? – mach Jun 06 '13 at 21:14
  • 2
    @mach: You're missing a valid copy of `android-support-v13.jar`, apparently. Just open up the JAR file (it's a ZIP archive, after all), and you should see `/android/support/v4/view/ViewPager.class`. – CommonsWare Jun 06 '13 at 21:43
  • @CommonsWare Well they certainly does not tell you in the tutorials! Thanks for this hint! – Petr Mar 03 '14 at 18:29
  • @CommonsWare Please add the comment about supporting "real" fragments by using android support v13 lib to your answer. For me it was the answer. – Martin Rajniak Mar 21 '14 at 09:18