简体   繁体   English

Android Vertical Listview,顶部水平滚动

[英]Android Vertical Listview with horizontal scrolling on top

I have an android app in which I have a multiple "Categories" each with some items associated with it. 我有一个Android应用程序,其中有多个“类别”,每个类别都有一些与之关联的项目。 Like an online electronics shop for instance where you would have categories such as Computers, Printers, cameras etc. as categories and then each of those would have some items in them. 就像在线电子商店一样,您可以将计算机,打印机,相机等类别作为类别,然后每个类别中都包含一些项目。

These categories should be displayed as "tabs" in the top of the screen where can select a tab to show which items is associated with it. 这些类别应在屏幕顶部显示为“选项卡”,在此处可以选择一个选项卡以显示与之关联的项目。 This works fine as it is but what I want is to be able to change category by sliding left or right on the screen as well. 这可以正常工作,但是我想要的是能够通过在屏幕上向左或向右滑动来更改类别。 选中的单个标签的图像(第一个)

As you can see on the image the first tab is selected. 如您在图像上看到的,第一个选项卡被选中。 Now what I need is when the user swipes right, tab 2 will be selected instead and all the items in the listview would be changed of course to those of category 2 (tab2). 现在我需要的是,当用户向右滑动时,将改为选择选项卡2,并且列表视图中的所有项目当然都将更改为类别2(选项卡2)。 All the items (independent of category) have the same design which is why I only use one listview. 所有项目(与类别无关)都具有相同的设计,这就是为什么我仅使用一个listview的原因。 Some of the problems I have faced are: 我遇到的一些问题是:

  • I don't know from the beginning how many tabs there are going to be so they have to be created at runtime. 我从一开始就不知道会有多少个选项卡,因此必须在运行时创建它们。

  • I can't just create a swipe listener on each item as the user would not be able to swipe if the items don't fill up the screen (as in the example below where there are only 4 items and an empty area below which would then have no listener) This also seems like it coul be performance heavy maybe ? 我不能仅在每个项目上创建滑动监听器,因为如果项目没有填满屏幕,用户将无法进行滑动(如下面的示例,其中只有4个项目,下面有一个空白区域然后没有听众)这似乎也可能会使性能沉重吗?

  • Adding a swipe listener on the entire view containing the list kind of works except that this view then intercepts ALL gestures including clicks which means the user will not be able to click a single item which is also required. 在包含列表类型作品的整个视图上添加滑动侦听器,不同的是此视图将拦截包括单击在内的所有手势,这意味着用户将无法单击单个项目,这也是必需的。

  • I thought about using a ViewPager but I have never used one before and I don't know if it is overkill as I basically just need "one" view but with different data depending on tab choice. 我考虑过使用ViewPager,但是我以前从未使用过它,我也不知道它是否过大,因为我基本上只需要“一个”视图,但是根据选项卡的选择使用不同的数据。

Hope anyone can help. 希望任何人都能提供帮助。

You're on the right track with a ViewPager, but to get the correct functionality for the top, you should use a library by Jake Wharton. 使用ViewPager的工作是正确的,但是要获得正确的顶部功能,应使用Jake Wharton的库。

https://github.com/JakeWharton/ViewPagerIndicator https://github.com/JakeWharton/ViewPagerIndicator

You can set your top elements, and the pager will correspond with a proper fragment in the adapted callback. 您可以设置顶部元素,并且传呼机将在改编的回调中与适当的片段相对应。

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

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