6

I would like to use horizontall scrolling items in a vertically scrolling Listview.

My naive take on this was to put the contents of the listview items inside a 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.

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, i.e 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.

Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34
Mikael_S
  • 141
  • 1
  • 2
  • 5

4 Answers4

6

Make ordinary ListView with any adapter you like but design the item Layout something like this:

<?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. And the items are scrolled independantly from other items.

Jilberta
  • 2,836
  • 5
  • 30
  • 44
3

You need to use a 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.

Felix
  • 88,392
  • 43
  • 149
  • 167
  • Doing that, you loose the ListView item click functionality. In other words, the click on the scrollable content is not forwarded to the Listview. – ol_v_er Feb 09 '12 at 16:59
2

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

from the android docs:

"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."

EDIT: it seems that the warning posted above is an error from the android documentation, talking with some colleagues they told me its possible. The issue in the documentation is here, http://code.google.com/p/android/issues/detail?id=2781.

my appologies

Community
  • 1
  • 1
Franco
  • 7,385
  • 3
  • 27
  • 22
  • Ok, but what is the alternative then? – Mikael_S Jan 31 '11 at 14:44
  • i talk with some colleagues and told me about an issue in the Android Documentation, i edited my post, appologies – Franco Jan 31 '11 at 15:02
  • I think that quote tries to say that you should not put a `ListView` inside a `ScrollView`, not the other way around, which should be fine. – Felix Jan 31 '11 at 15:37
0

You can use "ViewPager" each element in the list can be a ViewPager

Chris Sim
  • 4,054
  • 4
  • 29
  • 36