Android进阶2之Gallery无限循环

               


在此之前,我已经发表过常规的Gallery。如果之前没有学习过的,可以查看博文: Android学习笔记之Gallery  

这篇文章详细讲述了什么是gallery,以及怎么样是要用它。在这里就不用介绍了,这里主要讲述gallery的循环使用。

gallery的循环使用其实很简单的,上网一搜一大堆,但是我还是写一下。一是记录一下自己的学习成果,另一个是和大家一起分享一下。


实现gallery循环主要涉及到继承BaseAdapter的ImageAdapter 。

// 获取图片的个数  public int getCount()  {   return Integer.MAX_VALUE;  }


public View getView(int position, View convertView, ViewGroup parent)  {   ImageView imageView = new ImageView(context);   // 给ImageView设置资源   imageView.setImageResource(image[position % image.length]);   // 设置显示比例类型   imageView.setScaleType(ImageView.ScaleType.FIT_XY);   // 设置布局 图片120*80   imageView.setLayoutParams(new Gallery.LayoutParams(60, 60));   return imageView;  }


对比以前的文章,你会发现,变动了两处地方:一个是getCount()方法中返回了一个Integer的最大值代替了图片的数量;

另一个地方是imageView.setImageResource(image[position % image.length]);

gallery组件正好显示到最后一个图像,position参数值正好为getCount() - 1

为了实现了一个循环,必须return一个很大的数,就是保证点击一直可以往下走,对position 取余就实现了图片的循环0,1,2............image.length()-1,0,1,2.............



具体实现:

package xiaosi.loopgallery;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class LoopGalleryActivity extends Activityprivate Gallery gallery; private int[] image = new int[] { 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, }; public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  gallery = (Gallery) findViewById(R.id.gallery);  gallery.setAdapter(new ImageAdapter(this));  gallery.setSpacing(1);  gallery.setSelection(4);  gallery.setOnItemSelectedListener(new OnItemSelectedListener() {   public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)   {}   public void onNothingSelected(AdapterView<?> arg0)   {}  }); }   class ImageAdapter extends BaseAdapter {  private Context context;  public ImageAdapter(Context c)  {   context = c;  }  // 获取图片的个数  public int getCount()  {   return Integer.MAX_VALUE;  }  // 获取图片在库中的位置  public Object getItem(int position)  {   return position;  }  // 获取图片ID  public long getItemId(int position)  {   return position;  }  public View getView(int position, View convertView, ViewGroup parent)  {   ImageView imageView = new ImageView(context);   // 给ImageView设置资源   imageView.setImageResource(image[position % image.length]);   // 设置显示比例类型   imageView.setScaleType(ImageView.ScaleType.FIT_XY);   // 设置布局 图片120*80   imageView.setLayoutParams(new Gallery.LayoutParams(60, 60));   return imageView;  } }}


main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="30dp"         android:background="?android:galleryItemBackground"/></LinearLayout>



一起见证菜鸟的起飞。。。。。。



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/sdfsdfytre/article/details/86599987