Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top