3

I really like the template for Tabs+Swipe. You know...this one:

enter image description here

The problem is, I do not know where to start after creating that template. How to put a layout into each tab page?

Can we do something like call a Class when the tab is selected? So I can store the view for first tab in Class A then second tab in Class B.

I cannot found any information about this in Google, maybe because it is a new feature from Android SDK?

Thanks

hrsetyono
  • 4,474
  • 13
  • 46
  • 80
  • 1
    If you have downloaded the android support library from sdk manager then \android-sdks\extras\android\support\samples\Support4Demos\src\com\example\android\supportv4\app\FragmentTabsPager.java provide a good example on how to do that – Sreejith Krishnan R Sep 10 '12 at 16:06
  • @SreejithKrishnanR I downloaded that and import it to my workspace. But I got like 800 errors, mostly `cannot be resolved` error. Do you know how to fix that? – hrsetyono Sep 10 '12 at 16:20
  • I solved this issue by doing following 1.Right click imported project - Properties - Android - Project Build Target and setting android 4.1 as build target. 2.After that cleaning the project (Project - Clean) fixed all errors in the project – Sreejith Krishnan R Sep 10 '12 at 17:26

2 Answers2

3

How to put a layout into each tab page?

The generated code creates a ViewPager in the layout file and wires up action bar tabs to the pages in that ViewPager. Personally I'd rather use a PagerTabStrip, or possibly one of the ViewPagerIndicator equivalents, but they didn't ask me... :-)

Regardless, to fill in the pages, you need to replace (or complete) the SectionsPagerAdapter and/or DummySectionFragment inner classes of the generated activity. Right now, this is set up to have three pages, each of which is merely a TextView. But, you could have DummySectionFragment inflate a layout instead, and if you are looking for different layouts per tab, you would probably create separate Fragment implementations per layout/tab combination. You would then teach SectionsPagerAdapter how many pages you want (getCount()) and what to use for each page (getItem()) and what each tab caption is (getPageTitle()).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the answer. So I create a test.xml containing a button. Then I change the code of `onCreateView()` inside `DummySectionFragment` into `View view = getLayoutInflater(savedInstanceState).inflate(R.layout.test, container); return view;` But it crashed the template, any idea? – hrsetyono Sep 10 '12 at 16:34
  • 1
    @DarcCode: AFAIK, `getLayoutInflater()` does not take a parameter, so that code should not even compile. Your `onCreateView()` is *passed* a `LayoutInflater` -- use that one. Beyond that, look at your stack trace in LogCat. – CommonsWare Sep 10 '12 at 16:43
  • 1
    I changed the getLayoutInflater() to the passed LayoutInflater parameter. But it also crashed with this error: `java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.` What's the meaning? – hrsetyono Sep 10 '12 at 16:53
  • 2
    @DarcCode: Try `inflate(R.layout.test, container, false)`. Also, be sure that you are not reusing the same inflated `View` for multiple positions. – CommonsWare Sep 10 '12 at 16:57
  • Wow adding `false` make it works. But all tab have same layout. So should I create 3 different class that `extends Fragment`? Then inside `getItem()` should I create IF statement like `if(i==1)`? – hrsetyono Sep 10 '12 at 17:10
  • 1
    @DarcCode: "So should I create 3 different class that extends Fragment?" -- I cannot answer that, as is tied to your overall application logic and architecture. If all tabs should have the same business logic, just with different data, then a single `Fragment` class should suffice. Just return different instances from `getItem()`. For example, see: https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Fragments – CommonsWare Sep 10 '12 at 17:15
  • Thanks, +1 for all your comments – hrsetyono Sep 10 '12 at 17:24
0

Is it possible you can place an an example of activity switching between tabs? The example above isn't ideal.

Klinetel
  • 302
  • 1
  • 7
  • 27