1

I'm building an Android application which contains a fixed header with the application title in it. Right now, when the application changes activities, the header will slide out and slide in as well because both activity's have the same header. I really like this header to stay fixed at all times, even when switching activities.

I think this could easily be done by using a viewswitcher/viewflipper on the lay-out below the header and switch views only on that perticular part of the application. That would create my desired effect. The problem with this is, that every single screen I have in my application, would use the same java file. As I have around 12/13 different screens, all containing buttons textviews, listviews etc, that would create very messy code. I'd therefore prefer to use java files for each 'screen' but then I'd have to stick with different activity's and I can't keep the header on top during transistioning of activity's.

Is there a solution to actually keep a fixed header (and be able to animate the view/lay-out below) while using different java files for every application screen?

Niles11
  • 553
  • 1
  • 6
  • 17

1 Answers1

2

Work with Fragments, a FragmentStatePagerAdapter and a ViewPager. You could create a layout where you have the fixed header - and below that you have your ViewPager containing the different Fragments, each containing their own logic.

It's quite a change working with Fragments instead of only Activities, however I've found them very useful. For a starting point, check this link.

To not allow the user to swipe, but still being able to switch the viewpager item programatically, check out this tutorial and do your swiping using the setCurrentItem method.

Joakim Berglund
  • 2,859
  • 1
  • 22
  • 30
  • But Fragments are only supported from API Level 11 onwards (Android 3.0) while my application needs to work with level 2/2.1 or higher – Niles11 Apr 08 '12 at 22:06
  • 1
    No, check out the Compatibility Package, it enables you to use Fragments all the way back to 1.6. The starting point link points this out. – Joakim Berglund Apr 08 '12 at 22:08
  • Well it took me quite a bit of time but in the end I got it right. My application is using fragments now and I got the desired effect. But whats really baking my noodle right now is two things: using setCurrentItem doesn't create a smooth transistion effect (even with the boolean smoothScroll set to true). So instead of sliding in from the right, the view is loaded almost instantly. Is there a way to change that? The second problem is with dealing button onclicks. If I give a method name in XML (at onClick) it calls that method in the activity, not the fragment, which is quite annoying... – Niles11 Apr 09 '12 at 17:37