[英]Create infinite scrolling gallery View
I have a very simple Gallery
which I use to scroll through some pictures with a 2 seconds interval. 我有一个非常简单的Gallery
,可用于以2秒的间隔滚动浏览某些图片。 My question: how to make this gallery "infinite" so the first picture comes again after the last one? 我的问题: 如何使这个画廊“无限”,以便第一张照片在最后一张之后再次出现?
NOTE 注意
Main: 主要:
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();
}
});
}
Adapter: 适配器:
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;
}
}
You have to make custom Gallaryadapter by extending class.. 您必须通过扩展类来制作自定义Gallaryadapter。
I just found solution from here 我刚刚从这里找到解决方案
http://blog.blundell-apps.com/infinite-scrolling-gallery/ http://blog.blundell-apps.com/infinite-scrolling-gallery/
i may be helpfull for you; 我可能对你有帮助;
Also you can try this in your code.. 您也可以在代码中尝试此操作。
mHandler = new Handler();
r = new Runnable() {
public void run() {
if (count == adapter.getCount()) {
count = 0;
ga.setSelection(0);
mHandler.postDelayed(r, 2000);
} else {
mHandler.postDelayed(r, 2000);
ga.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
count++;
}
}
};
r.run();
well, actually the solution is much easier than I thought. 好吧,实际上解决方案比我想象的要容易得多。 It might not be suitable for everyone but it's OK in my case. 它可能并不适合所有人,但就我而言还是可以的。 Just added a Random to the getView()
method 刚刚向getView()
方法添加了一个Random
Random rnd = new Random();
if(position>=pics.length){
position = rnd.nextInt(13);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.