Android horizontal scroll list [duplicate]
Asked Answered
F

6

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 :)

Fadden answered 4/4, 2011 at 10:38 Comment(1)
Add some code related information else this post can't be helpfulRheometer
O
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

Obadiah answered 4/4, 2011 at 10:40 Comment(4)
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. ThanksFadden
I have implemented Horizontal list as part of my UI library. Code is very clean and simple. You can find it here github.com/applm/ma-componentsBurchfield
@Burchfield Can you please provide a way to import your library to Android Studio ?Photoreceptor
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.Burchfield
O
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!!

Originality answered 4/4, 2011 at 10:46 Comment(7)
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 GalleryFadden
Updated my answer. sorry for the first answer.. i didnt read your question properly.. :(Originality
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.Fadden
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. :POriginality
Seems to be a pretty common question on SO, looky what i found: #3240831 which points to someone who has implemented a horizontal scrolling ListView, maybe you can use at least as a starting point: dev-smart.com/?p=34Obadiah
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 answerFadden
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 :).Obadiah
Q
2
 <HorizontalScrollView>
   ....Horizontal LinearLayout here....
</HorizontalScrollView>

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

Quartered answered 4/4, 2011 at 10:46 Comment(3)
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.Fadden
Oh well, then if Android doesn't offer one i guess you have to write it yourself :)Obadiah
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. ThanksFadden
S
1
<HorizontalScrollView>
....your layout here....
</HorizontalScrollView>
Susysuter answered 4/4, 2011 at 10:39 Comment(2)
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. ThanksFadden
Simply put a <LinearLayout> inside the <HorizontalScrollView>, then add the children to the <LinearLayout>Susysuter
D
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? :-)

Dissection answered 2/4, 2012 at 15:39 Comment(0)
I
0

Cant you change the android:gravity in Gallery?

Intramural answered 4/4, 2011 at 10:43 Comment(1)
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 GalleryFadden

© 2022 - 2024 — McMap. All rights reserved.