Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate infinite scrolling gallery View
    primarykey
    data
    text
    <p>I have a very simple <code>Gallery</code> which I use to scroll through some pictures with a 2 seconds interval. My question: <em>how to make this gallery "infinite" so the first picture comes again after the last one?</em></p> <p><strong>NOTE</strong></p> <ul> <li>I'm aware of the fact that Gallery is claimed deprecated</li> <li>I did not perform any optimizations to my code yet since that is just a quick test on a high-end tablet. Though your suggestions regarding optimization are also appreciated</li> </ul> <p>Main:</p> <pre><code> public class MainActivity extends Activity { private Gallery ga; private Runnable r = null; private Handler mHandler = null; private MediaPlayer mp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); setUpGallery(); mHandler = new Handler(); r = new Runnable() { public void run() { mHandler.postDelayed(r, 2000); ga.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null); } }; r.run(); } private void setUpGallery() { ga = (Gallery) findViewById(R.id.gallery); ga.setAdapter(new ImageAdapter(this)); mp = MediaPlayer.create(getApplicationContext(), R.raw.you); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.reset(); mp.release(); } }); } </code></pre> <p>Adapter:</p> <pre><code> public class ImageAdapter extends BaseAdapter { private int[] pics = { R.drawable.s1, R.drawable.s2, R.drawable.s3, R.drawable.s4, R.drawable.s5, R.drawable.s6, R.drawable.s7, R.drawable.s8, R.drawable.s9, R.drawable.s10, R.drawable.s11, R.drawable.s12, R.drawable.s13, R.drawable.s14 }; private Context ctx; public ImageAdapter(Context c) { ctx = c; } @Override public int getCount() { return pics.length; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = new ViewHolder(); holder.img = new ImageView(ctx); Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), pics[position]); holder.img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(bmp, 10)); return holder.img; } static class ViewHolder { public ViewHolder() { } ImageView img; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload