Android: how to do infinite scroll with infinite scroll

I want the ScrollView code ScrollView show the same images with endless scrolling over and over again.

This is great for layout, and I want to know what code is needed to add it to ScrollView with infinite scroll.

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

+6
source share
2 answers

Use ListView and Adapter , which are slightly modified to have "infinite" elements

Here are the changes to your 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 are fooling him by telling him that the counter is MAX_INT , and then when you go to get a way to use the object to get the correct element in the sequence.

Several people have suggested other solutions for this already.

See here: Android Endless List

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

+7
source

FoamGuy's answer is correct. But in order to make the list back, as in an infinite carousel, I also fool the system by setting the default element Integer.MAX_VALUE / 2 by calling:

 listView.setSelection( Integer.MAX_VALUE/2 ); 

It is very unlikely that the user will scroll back 1 billion items back, which makes the effect of an endless carousel in both directions.

I also have some other changes to the custom BaseAdapter 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 } ... } 

Thus, the list will rotate as in a carousel without additional memory allocation for unnecessary presentations.

+5
source

Source: https://habr.com/ru/post/956325/


All Articles