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.