简体   繁体   English

如何在垂直列表视图中制作可水平滚动的项目?

[英]How can I make horizontally scrollable items in a vertical listview?

I would like to use horizontall scrolling items in a vertically scrolling Listview. 我想在垂直滚动的Listview中使用horizo​​ntall滚动项。

My naive take on this was to put the contents of the listview items inside a scrollView. 我天真的想法是将listview项的内容放在scrollView中。 The items are wider horizontally than the scrollview, but not higher than the scrollview. 项目在水平方向上比滚动视图宽,但不高于滚动视图。 Since the listview is a normal vertically scrolling listview, I figured that dragging vertically would scroll in the list, while dragging horizontally would scroll in the items. 由于listview是一个普通的垂直滚动列表视图,我认为垂直拖动会在列表中滚动,而水平拖动会在项目中滚动。

However that didn't work. 但是那没用。 The list scrolls fine vertically and shows the items correctly, but scrolling horizontally does not work (nothing happens). 列表垂直滚动并正确显示项目,但水平滚动不起作用(没有任何反应)。 Unfortunately I am really not sure where to go from here. 不幸的是,我真的不确定从哪里开始。

Note that the items should scroll horizontally independently of the other items, ie the whole list should not scroll sideways when dragging sideways. 请注意,项目应独立于其他项目水平滚动,即横向拖动时整个列表不应向侧面滚动。

As a reference, I would like the list to behave similar to what it does in the app 'Pulse', in case you have seen it. 作为参考,我希望该列表的行为类似于它在应用程序'Pulse'中的行为,以防您看到它。

Make ordinary ListView with any adapter you like but design the item Layout something like this: 使用您喜欢的任何adapter制作普通的ListView ,但设计项目布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <HorizontalScrollView
        android:id="@+id/hor_scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/lin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginRight="6.0dip"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true"
                android:focusable="false" />

            <TextView
                android:textAppearance="?android:textAppearanceMedium"
                android:gravity="center_vertical"
                android:id="@+id/text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:singleLine="true"
                android:layout_toRightOf="@id/icon"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true" />

        </LinearLayout>

    </HorizontalScrollView>


</RelativeLayout>

You'll have Vertically Scrollable ListView with Horizontally Scrollable items. 您将拥有具有水平可Scrollable项目的垂直可Scrollable ListView And the items are scrolled independantly from other items. 并且这些项目与其他项目无关地滚动。

You need to use a HorizontalScrollView . 您需要使用HorizontalScrollView Droidstack is an open source app (I wrote) that does just this (in the questions lists you can scroll the tags on each question), if you want a concrete example. Droidstack是一个开源应用程序(我写的)就是这样做(在问题列表中你可以滚动每个问题上的标签),如果你想要一个具体的例子。

if you follow the good practices of android development, you should never put a ScrollView inside a ListView, is unrecomended bu Romain Guy and other people from android development, to read the arguments read here: Android ScrollView layout problem 如果你按照android开发的好习惯,你应该永远不要把一个ScrollView放在一个ListView中,是不可思议的bu Romain Guy和其他人从android开发,阅读这里读取的参数: Android ScrollView布局问题

from the android docs: 来自android文档:

"You should never use a HorizontalScrollView with a ListView, since ListView takes care of its own scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by HorizontalScrollView." “你永远不应该使用带有ListView的Horizo​​ntalScrollView,因为ListView负责自己的滚动。最重要的是,这样做会使ListView中的所有重要优化都无法处理大型列表,因为它有效地强制ListView显示其整个列表用于填充Horizo​​ntalScrollView提供的无限容器的项目。“

EDIT: it seems that the warning posted above is an error from the android documentation, talking with some colleagues they told me its possible. 编辑:似乎上面发布的警告是来自android文档的错误,与他们告诉我可能的一些同事交谈。 The issue in the documentation is here, http://code.google.com/p/android/issues/detail?id=2781 . 文档中的问题在此处, http://code.google.com/p/android/issues/detail?id = 2781

my appologies 我的appologies

您可以使用“ViewPager”列表中的每个元素都可以是ViewPager

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

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