Gallery控件
一个画廊视图,用于展示一组图片,用固定在中间位置的水平滚动列表显示列表项的视图。
Gallery的常用XML属性:
- android:animationDuration:设置列表项切换时的动画持续时间,使用毫秒为单位。
- android:gravity:设置列表项的对其方式。
- android:spacing:设置Gallery内列表项之间的间距。
- android:unselectedAlpha:设置没有被选中的列表项的透明度,范围是一个为0~1的float数,越接近0越透明
作为一个列表框,声明的事件是定义在AdapterView类中,常用事件有如下几个:
- AdapterView.OnItemCLickListener:列表项被点击时触发。
- AdapterView.OnItemLongClickListener:列表项被长按时触发。
- AdapterView.OnItemSelectedListener:列表项被选择时触发
Gallery数据绑定
Gallery的Adapter适配器,虽然可以直接实现Adapter接口,但是一般推荐继承另外一个抽象类:BaseAdapter,它已经实现了一些常用的方法。对于BaseAdapter而言,继承它必须要实现几个方法,下面先了解一下这几个方法:
- int getCount():当前适配器中存在多少个数据项。
- Object getItem(int position):返回数据集中指定位置的数据项。
- long getItemId(int position):返回数据集中指定位置的数据行Id。
- View getView(int position,View convertView,ViewGroup parent):返回一个视图,用于显示数据集中指定位置的数据项。
对于BaseAdapter的getView()方法,这里着重讲解一下。在绑定Adapter适配器之后,系统会根据getCount()方法返回的数据项,循环调用getView()方法,用于返回每个position位置的显示视图,所以对于一个Adapter适配器,其主要的代码量就在getView()方法中。因为BaseAdapter的getView()方法返回的是一个View数据,所以一般自定义的展示效果都会用到BaseAdapter来做父类,继承实现其中的方法来实现自定义的View来展示在UI界面上。
布局代码:
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Gallery Demo" > </TextView> <!-- 定义一个Gallery,其中动画步进3秒,数据项间隔2dp,透明度为50% --> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:animationDuration="3000" android:spacing="2dp" android:unselectedAlpha="0.5" /> <ImageView android:id="@+id/imageview" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ImageView> </LinearLayout>
实现代码:
package com.leaf.android; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class Main extends Activity { /** Called when the activity is first created. */ private Gallery gallery; private ImageAdapter imageAdapter; private ImageView imageView; // 声明图片的数组 private int[] resIds = new int[] { R.drawable.car1, R.drawable.car2, R.drawable.car3, R.drawable.car4, R.drawable.car5, R.drawable.car6, R.drawable.car7, R.drawable.car8, R.drawable.car9, R.drawable.car10, }; // android的适配器 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gallery = (Gallery) this.findViewById(R.id.gallery); imageView = (ImageView) this.findViewById(R.id.imageview); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub imageView.setImageResource(resIds[position]); } }); } // 声明一个BaseAdapter public class ImageAdapter extends BaseAdapter { private Context context;// 使用Adapter的上下文变量 int mGralleyItemBackground;// 使用简单的计数器,填充背景图片 public ImageAdapter(Context context) { this.context = context; // 读取属性 TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); mGralleyItemBackground = typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); typedArray.recycle(); } public int getCount() { // 返回数据集中数据的个数 return resIds.length; } public Object getItem(int position) { // 返回数据集中当前podition位置的数据 return resIds[position]; } public long getItemId(int position) { // 返回数据集中当前podition位置的数据的Id return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // 自定义的适配器,需要用自定义的布局来显示,通常android的通用布局是不能满足我们的需求 // 可以手工创建一个view视图,也可以用inflate填充一个xml // 从数据源中根数position获得每一个item的值,填充到指定的xml布局中 // View convertView 是一个旧的布局,如果没有新的布局填充的时候,将使用旧的布局 // 当前的布局会被追加到父布局中 if (convertView == null) { imageView = new ImageView(context); imageView.setImageResource(resIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(160, 120)); } else { imageView = (ImageView) convertView; } imageView.setBackgroundResource(mGralleyItemBackground); return imageView; } } }
在代码中用到了一个XML文件定义的Android样式,这里仅仅是为了用于Gallery展示图片的背景样式,并不是必须的。下面是样式的XML资源文件attrs.xml的代码,文件存放地址为res/values中。
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Gallery"> <attr name="android:galleryItemBackground"></attr> </declare-styleable> </resources>