15

Possible Duplicate:
Horizontal ListView in Android?

I want horizontal scroll like Gallery. Im not using Gallery because its center locked.

Can some one here would help me out with this So I can have horizontal scrolling list?

I think best example of this is pulse news reader :- https://market.android.com/details?id=com.alphonso.pulse

Thanks :)

Community
  • 1
  • 1
Umakant Patil
  • 2,227
  • 6
  • 32
  • 58

6 Answers6

11

http://developer.android.com/reference/android/widget/HorizontalScrollView.html ?

EDIT: Ok after finding out i don't need to do a RTFM post i searched around a bit and this has been asked before here:

Horizontal ListView in Android?

And has already been implemented here:

http://dev-smart.com/?p=34

Community
  • 1
  • 1
Torp
  • 7,924
  • 1
  • 20
  • 18
  • HorizontalScrollView can only contain one child. I want many child's e.g.Gallery can have many child's and Vertical ListView can also have many childs. I want same way but horizontal instead of vertical. Thanks – Umakant Patil Apr 04 '11 at 10:44
  • I have implemented Horizontal list as part of my UI library. Code is very clean and simple. You can find it here https://github.com/applm/ma-components – snapix Jun 19 '14 at 11:12
  • @snapix Can you please provide a way to import your library to `Android Studio` ? – Menelaos Kotsollaris Jan 22 '15 at 16:28
  • Hi, Since library is not maintained regularly. I recommend you just copy source files you are interested in into your project. You will be able to modify and customize them. – snapix Jan 26 '15 at 08:52
4

I have created a view with the help of ScrollView and HorizontalScrollView to give you atleast a start. Here is the XML of that code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:padding="5dp">

        <HorizontalScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/a" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:padding="5dp"
                android:layout_weight="1">
            </LinearLayout>
        </HorizontalScrollView>

        <HorizontalScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/b" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:padding="5dp"
                android:layout_weight="1">
            </LinearLayout>
        </HorizontalScrollView>

        <HorizontalScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/c" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:padding="5dp"
                android:layout_weight="1">
            </LinearLayout>
        </HorizontalScrollView>

        <HorizontalScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/d" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:padding="5dp"
                android:layout_weight="1">
            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>
</ScrollView>

Plus java code:

public class SampleActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a);
        LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.b);
        LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.c);
        LinearLayout linearLayout3 = (LinearLayout) findViewById(R.id.d);

        for (int i = 0; i < 10; i++) {

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.sample_0);
            linearLayout.addView(imageView);

            ImageView imageView1 = new ImageView(this);
            imageView1.setImageResource(R.drawable.sample_1);
            linearLayout1.addView(imageView1);

            ImageView imageView2 = new ImageView(this);
            imageView2.setImageResource(R.drawable.sample_2);
            linearLayout2.addView(imageView2);

            ImageView imageView3 = new ImageView(this);
            imageView3.setImageResource(R.drawable.sample_3);
            linearLayout3.addView(imageView3);
        }
    }

}

I tried uploading some screenshots also but i guess it was blocked by my network firewall. so will upload them later.

Hope this helps!!

mudit
  • 25,306
  • 32
  • 90
  • 132
  • Gallery's selected item is always center locked. If you click the left side item, then it brings it in center. I don't want that functionality. Selected item should stay there and be selected. So was the reason for not using Gallery – Umakant Patil Apr 04 '11 at 11:02
  • Updated my answer. sorry for the first answer.. i didnt read your question properly.. :( – mudit Apr 04 '11 at 11:09
  • Thanks for the efforts mudit. Can we have something like Gallery or ListView, i.e. I can set adapter. Adapter reuses the view. If I insert all views in LinearLayout then If list goes long, It make the app slow or some times crash as I have images and text as the item. Your code is ok for short list which has text only. – Umakant Patil Apr 04 '11 at 11:22
  • 1
    I guess, now you have to make some effort to write whatever you want, this just an idea on how you can get a start. :P – mudit Apr 04 '11 at 11:24
  • 1
    Seems to be a pretty common question on SO, looky what i found: http://stackoverflow.com/questions/3240331/horizontal-listview-in-android which points to someone who has implemented a horizontal scrolling ListView, maybe you can use at least as a starting point: http://www.dev-smart.com/?p=34 – Torp Apr 04 '11 at 13:02
  • Torp could you post these as a separate answer, So I can select it as Correct. Other people searching may not look into comment and will not find answer – Umakant Patil Apr 04 '11 at 13:48
  • I edited my original answer to include the other info, seemed cleaner that way (besides SO nudged me to do that when i tried to post a 2nd answer :). – Torp Apr 04 '11 at 13:53
2
 <HorizontalScrollView>
   ....Horizontal LinearLayout here....
</HorizontalScrollView>

Inside the Horizontal LinearLayout you can add all your views...

Umesh
  • 4,406
  • 2
  • 25
  • 37
  • Buddy, It should be some thing like Gallery or ListView, i.e. I can set adapter. Adapter reuses the view. If I insert all views in LinearLayout then If list goes long, It make the app slow as I have images and text as the item. – Umakant Patil Apr 04 '11 at 10:49
  • Oh well, then if Android doesn't offer one i guess you have to write it yourself :) – Torp Apr 04 '11 at 10:59
  • Yeah, I thought If some one has done it then I can implement it directly instead of reinventing the wheel again And I'm very much new to android, So don't how I will go with it. Thanks – Umakant Patil Apr 04 '11 at 11:03
1
<HorizontalScrollView>
....your layout here....
</HorizontalScrollView>
DKIT
  • 3,471
  • 2
  • 20
  • 24
  • HorizontalScrollView can only contain one child. I want many child's e.g.Gallery can have many child's and Vertical ListView can also have many childs. I want same way but horizontal instead of vertical. Thanks – Umakant Patil Apr 04 '11 at 10:44
  • 1
    Simply put a inside the , then add the children to the – DKIT May 31 '11 at 11:33
1

Download Pulse apk, decompile using dex2jar + JD-GUI and take a look how they do it!

Excerpt:

package com.alphonso.pulse.views;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.Gallery;

public class HorizontalTileView extends Gallery
{
... etc

Can a kind gentleman/lady later use this to write us all a tutorial on the implementation? :-)

YiEn
  • 11
  • 1
0

Cant you change the android:gravity in Gallery?

KilledKenny
  • 385
  • 4
  • 19
  • Gallery's selected item is always center locked. If you click the left side item, then it brings it in center. I don't want that functionality. Selected item should stay there and be selected. So was the reason for not using Gallery – Umakant Patil Apr 04 '11 at 10:46