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.
source share