简体   繁体   English

如何将ViewPager添加为ListView的顶部项

[英]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). 我有一个普通的“新闻”应用程序,其中的主页是一个ListView,它垂直滚动着文章列表(缩略图,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. 我认为它应该在列表中,但是对于Android来说是相当陌生的,并且不知道是否有更好的解决方案。

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. 我正在将ViewPager用于我的图库视图(当他们单击照片时),这似乎工作得很好...但是那是整个视图...并且该视图已经有一个填充listView的适配器...我希望我只是使某些事情复杂化了。

TLDR : 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" 注意:android:minSdkVersion =“ 8” android:targetSdkVersion =“ 15”

You can use addHeaderView() from the ListView API to set a header. 您可以使用ListView API中的addHeaderView()设置标题。 It can be any View, so also a ViewPager. 它可以是任何View,也可以是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是一个与其他视图ViewPager的视图,因此在ListView的适配器的getView()方法中,返回一个包含ViewPager的视图。

ViewPager is a topic in itself, so if your problem is that you don't understand ViewPager , read these links, ViewPager本身就是一个主题,因此,如果您的问题是您不了解ViewPager ,请阅读以下链接,

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

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

if you are using API level 8, you should use the ViewPager and friends from the compatibility package. 如果使用的是API级别8,则应使用兼容性包中的ViewPager和朋友。

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. 我知道ViewPager包含的代码可以将任何手势传递给子视图,如果它决定无法处理它们的话。 i can't say if ListView has the same smarts. 我不能说ListView是否具有相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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