8

Is there a way to add a viewpager as a listview item?

I want to add it as a "header" on top of my listview before all real list items follow. I tried using addHeaderView() or declaring multiple view types in my list adapter, however, the viewpager never shows up whatever way I try.

Has anyone done something similar? Help!

1
  • Would have been good to include your original layout code.
    – Dallas
    Apr 30, 2013 at 20:01

2 Answers 2

12

Do not use wrap_content, try to calculate and set ViewPager size programmatically and then add ViewPager as a header of ListView.

2
  • this gives poor results too Sep 20, 2013 at 10:02
  • Yes this way it works. I don't know why it didn't work for @RafaelSanches
    – Sufian
    Jan 2, 2015 at 5:50
1

Quoting Dianne Hackborn, ViewPager is "just not intended to be used as an item in a list"

9
  • I read that, but I found out that ViewPager does not support wrap_content and thats why it does not show up. If I explicitly set width/height in the layout params then it works. I don't want every item in the list to be a viewpager, just the top "header" one as I use it for a "gallery" type of view for promoted items.
    – dnkoutso
    Dec 29, 2011 at 23:28
  • 2
    @dnkoutso: Ms. Hackborn is one of Android's core engineers. She might have even written ViewPager. I humbly suggest that you find some other solution. For example, you might use Gallery for a gallery. Or use HorizontalScrollView. Putting scrollable things in scrollable things has historically been troublesome, but if a Googler steers you away from a solution, you would be well advised to honor the suggestion. Dec 29, 2011 at 23:36
  • Gallery is a very old widget not suitable for my needs. At the moment I use a ViewFlipper which does the job well but I would prefer the smoothness and functionality of a ViewPager. I understand the concern of blending the two, but I dont see a problem with it as long as I specify the dimensions I want the viewpager to be. p.s I know who Dianne is :)
    – dnkoutso
    Dec 30, 2011 at 0:33
  • 23
    Not sure why this is the accepted answer. This is commentary and doesn't answer the asker's question.
    – Dallas
    Apr 30, 2013 at 19:58
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.