简体   繁体   中英

How do I add a ViewPager as the top item in my ListView

I have a vanilla "News" app, where the main page is a ListView that scrolls vertically with a list of articles (thumbnail, title..etc). This works just fine, but now I want to add a horizontal article/photo rotator as the top item in that list. I don't really care if it's technically in the list or not, but it should scroll up/off the screen when the list is dragged...etc. I assume(d) it should be in the list, but am quite new to Android and don't know if there's a better solution.

After an entire day of searching around, I'm still empty-handed - don't know how to add it or even if it's the right method. I'm using a ViewPager for my gallery view (when they click a photo), and that seems to be working fine...but that's the entire view... and this one already has an adapter that populates the listView... I hope I'm just over-complicating something.

TLDR :

1) Can I add a ViewPager as top item in a ListView?
2) How would one go about doing it?
3) Is there a better solution?

Note: android:minSdkVersion="8" android:targetSdkVersion="15"

You can use addHeaderView() from the ListView API to set a header. It can be any View, so also a ViewPager. I think you'll have to add it programmatically. Set the height of it correctly though.

i'm not sure i understand your question. ViewPager is a view like any other, so in your ListView 's adapter's getView() method, return a view that contains a ViewPager .

ViewPager is a topic in itself, so if your problem is that you don't understand ViewPager , read these links,

http://developer.android.com/reference/android/support/v4/view/ViewPager.html

http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

if you are using API level 8, you should use the ViewPager and friends from the compatibility package.

that all being said, i wonder if you will have trouble putting a scrollable view inside a scrollable view. i know ViewPager contains code to pass off any gestures to child views if it decides it cannot handle them. i can't say if ListView has the same smarts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM