android:how to make infinite scrollview with endless scrolling
Asked Answered
S

2

7

I want a code to make the ScrollView show the same images with endless scrolling over and over.

This is excelent for the layout and I want to know what is the code necessary for adding it to a ScrollView with infinite scrolling.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"

    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
           <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
           <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


    </LinearLayout>
</HorizontalScrollView>

Slumlord answered 19/10, 2013 at 17:42 Comment(1)
possible duplicate of Android Endless ListMyrilla
M
10

Use a ListView and an Adapter that is modified slightly to have "infinite" elements

Here are the changes to your the Adapter that would support this behavior:

@Override
public int getCount()
{
    return Integer.MAX_VALUE;
}

@Override
public ImageItem getItem(int position) 
{
    return mItems.get(position % mItems.size());
}

Essentially you trick it by telling it that the count is MAX_INT and then when you go to get an item use mod to get correct item in the sequence.

Several people have proposed different solutions to this already as well.

See Here: Android Endless List

and CommonsWare has a component that supports this behavior as well: https://github.com/commonsguy/cwac-endless

Mc answered 19/10, 2013 at 17:51 Comment(4)
aaah, the good old modulo!! this is far the best solution for an infinite picker or listview I found!Ricky
but what if i dont want listview, I want endless scrollview?Gipson
@usman scrollview by itself is endless. whatever you have inside it must be endless too though if you want that behavior.Mc
@Mc I meant, how to create LinearLayout within a scrollview such that the scroll is not limited by the number of items in the LinearLayout. i.e. The last item in the LinearLayout appears connected to the first item. Sorry i probably didn't make myself clear enough.Gipson
P
5

FoamGuy's answer is correct. But in order to make the list go backwards, as in an infinite carrousel, I also trick the system by setting the default element to be Integer.MAX_VALUE/2 by calling:

listView.setSelection( Integer.MAX_VALUE/2 );

It is pretty unlikely the user will scroll back 1 billion elements back, which makes the effect of infinite carrousel in both directions.

I also have some other modifications to the BaseAdapter custom implementation:

@Override
public Object getItem(int position) {
    if ( model.getSize()==0 ) {
        return null;
    }

    // mod the list index to the actual element count
    return model.getElementAtPosition( position%model.getSize() );
}

@Override
public long getItemId(int position) {
    if ( model.getSize()==0 ) {
        return -1;
    }

    // same as above
    return model.getElementAtPosition( position%model.getSize() ).id;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if ( model.getSize()==0 ) {
        return null;
    }

    // also make sure to point to the appropriate element in your model otherwise you 
    // would end up building millions of views.
    position%=model.getSize();

    View front= convertView;

    if ( convertView==null ) {
        // inflate/build your list item view
    }

    ...
}

This way the list will spin around like in a carousel w/o extra memory allocation for unneeded views.

Pinery answered 18/1, 2014 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.