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!

dnkoutso
  • 6,041
  • 4
  • 37
  • 58

2 Answers2

12

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

Wayne
  • 6,361
  • 10
  • 46
  • 69
1

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

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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 '11 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. – CommonsWare Dec 29 '11 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 '11 at 00:33
  • hi is it possible to add view pagger in header , is there any example, Ive tried wont display anything, any examples. – user606669 Jun 29 '12 at 15:28
  • 23
    Not sure why this is the accepted answer. This is commentary and doesn't answer the asker's question. – Dallas Apr 30 '13 at 19:58
  • 1
    http://dallasgutauckis.com/2013/04/30/android-putting-a-viewpager-inside-a-listview/ – Randy Sugianto 'Yuku' May 07 '13 at 03:23
  • @CommonsWare I want to make a image slider (carousel) inside a recyclerview. Can I use viewPager. If not, then how can I make the clid imageViews of HorizontalScrollView to match parent (only one image on screen at a time). android:fillViewport="true" only works for one child view. – Farooq Khan Jul 02 '17 at 20:36
  • @FarooqKhan: A more typical approach here would be nested `RecyclerViews`. If you wanted a `ViewPager` with views for the pages, that might work. I would be *very* hesitant to use a `ViewPager` with fragments for the pages. I would not use `HorizontalScrollView` for your use case. – CommonsWare Jul 02 '17 at 20:47
  • @CommonsWare Thanks for your response. I am also using HorizontalScrollView but how can I make the width of all child views to match_parent. As doing android:fillViewport="true" only works if LinearLayout inside HorizontalScrollView has only one child. – Farooq Khan Jul 03 '17 at 04:27