Monday, September 5, 2011

Android Infinite Looping Gallery


Hey there,
For a very long time i had been watching people struggling for developing a Infinite Looping Gallery in Android.

Saw very different solutions... some seriously complicated and some ... Not even close to solutions. So, drafted a small snippet which details about how you can create your (Virtually) Infinite Gallery.

Virtually -> No we are not going to use any thing like Integer.MAX_VALUE for number of elements in the list. No crazy circular linked list or anything of that sort and yes no copy pasting from Android Source.

Enough talk, lets code :D

All you need is a Gallery from android and some small peice of code.

Associate this listener to your Gallery

/**
* Copyright [2011] [Abhinava Srivastava]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

// Constants
public final static int ADDED_EXTRA = 6;
public final static int[] mImageIds = {R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.d,R.drawable.e,R.drawable.f,
R.drawable.g,R.drawable.h,R.drawable.i,
R.drawable.j,R.drawable.k,R.drawable.l,
R.drawable.m,R.drawable.n};


// Associate it to your gallery.

private OnItemSelectedListener listner = new OnItemSelectedListener() {


public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 < ImageAdapter.ADDED_EXTRA / 2)
((Gallery) findViewById(R.id.gallery1)).setSelection(
mImageIds.length + 2, false);
if (arg2 > mImageIds.length + ImageAdapter.ADDED_EXTRA / 2) {
((Gallery) findViewById(R.id.gallery1)).setSelection(4, false);
}
}


public void onNothingSelected(AdapterView<?> arg0) {
((Gallery) findViewById(R.id.gallery1)).setSelection(3, false);
}
};

In your Adapter just need to modify 2 places.

/**
* Copyright [2011] [Abhinava Srivastava]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/


public int getCount() {
return mImageIds.length + ADDED_EXTRA;
}



public View getView(int position, View convertView, ViewGroup parent) {
if (position < ADDED_EXTRA / 2) {
position = GalleryTestActivity.mImageIds.length
- (ADDED_EXTRA / 2 - position);
} else if (position >= (GalleryTestActivity.mImageIds.length + (ADDED_EXTRA / 2))) {
position = position
- (GalleryTestActivity.mImageIds.length + (ADDED_EXTRA / 2));
} else {
position -= (ADDED_EXTRA / 2);
}
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.items, parent, false);
}
try {
((ImageView) view)
.setImageResource(GalleryTestActivity.mImageIds[position]);
} catch (Exception e) {
Log.i("ARRAY INDEX", "Position" + position, e);
}
return view;
}



yeah that's it.. nothing more... you are ready with you infinite looping circular gallery.

9 comments:

Unknown said...

This is awesome and works perfectly ! Subscribing to your blog ;)

Anonymous said...

I dont think this is going to work, as u mentioned the ADDED_EXTRA =6 in getCount(), so its gono show only 6 more such loops, if u need infinite loop then Add Integer.MAX_VALUE; in getCount() by this its asumes u have max value and keep looping until it.

Unknown said...

HI anonymus...
it works :)
Why dont you give it a try by running the code.. m using it in my apps

noema said...

Hi,

I managed to make it work.

Is there a way to have it programatically scroll to a specified position?

Thanks
Nghia.

Anonymous said...

Hi.

From my test, it is not working well.
Did you really verify this whether works everytime?

This code give a insight, but it not really working everytime. for example more than six items, or in customized adapter.

Unknown said...

The current sample is for maximum 6 elements.
Its not he sudo code its the working code :)

Unknown said...

thanks Abhinava ,this is 100% working it made some enhancement in this code..soom publish it....

SreeRamTej said...

please post full source code

Anonymous said...

1. implementation can be improved in the selection listener..
2. not working for item counts < ADDED_EXTRA/2