一、 Gallery的监听事件
Gallery的两个重要监听事件如下:
1、OnItemClickListener 监听事件
说明:当Gallery中的Item处于选中状态并且被点击触发该事件 ;
其监听方法为:
public voidonItemClick(AdapterView< > parent, View view, int position, long id)
2、OnItemSelectedListener 监听事件
说明:当Gallery中的Item处于选中状态时触发该事件
其监听方法为:
public voidonItemSelected(AdapterView< > parent, View view, int position, long id)
说明:当Gallery中的Item处于选中状态时触发该事件
public void onNothingSelected(AdapterView< > parent)
说明:当控件没有任何一项item选中时,触发该方法
两种监听事件的区别在于,Item被选中(selected)的由来。其由来有两种:
1、鼠标点击(click)了Item (先click),然后该项selected ;
2、代码设置某项Item 选中,例如setSelection(int position)(具体使用见下文) ,然后该项selected .
在情形1时,首先触发OnItemClickListener(先click),接着便是OnItemSelectedListener监听(因为item selected)。当某个Item
处于选中状态时,如果它是由情形2而来,就不会触发OnItemClickListener监听(没有click),只会触发OnItemSelectedListener监听
(只是selected)。
二、Gallery的setSelection()方法
方法原型: public void setSelection (int position)
说明:使第position处于选中状态。同时触发OnItemSelectedListener监听事件。
PS: 在listView控件中也存在setSelection()是让该行处于屏幕可见状态,不需要手动滑动定位。第一次进入界面时,
默认显示第一行,于是乎,我们可以手动设置该方法,ListView在显示时,便可主动定位该item了。
=====================我就是一个可爱的分割====================================
步骤一:开启一个线程,循环遍历图片集的资源id,并且将id发送至Hanlder对象。
步骤二:Handler接受到当前图片资源的ID,调用setSelection (id)选中它(该Item selected),继而setSelection()
触发OnItemSelectedListener 事件,执行目标方法,这样我们的目的就达到了。
代码如下:
package com.itarchy.gridview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
/**
*
* @E-mail: itarchy@163.com
* @version 创建时间:2015-11-6上午11:32:02
* @Des:
*/
@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements ViewFactory {
private Gallery mGallery;
private MygalleryAdapter mygalleryAdapter;
// references to our images
private Integer[] mThumbIds = null;
private ImageSwitcher mImageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDataSource();
// 获取控件
mGallery = (Gallery) this.findViewById(R.id.gallery);
mGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mImageSwitcher.setImageResource(mThumbIds[position]);
}
});
// 创建一个视图切换器
mImageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
// 切换器上面不能直接显示图片
// 需要实现这个接口,然后实现makeView()创建一个控件
mImageSwitcher.setFactory(this);
initAdapter();
}
// 1 初始化数据源
private void initDataSource() {
mThumbIds = new Integer[] { R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7 };
}
private void initAdapter() {
mygalleryAdapter = new MygalleryAdapter(this, mThumbIds);
mGallery.setAdapter(mygalleryAdapter);
mGallery.setSelection(mThumbIds.length / 2);
}
@Override
public View makeView() {
// 因为ImageSwitcher控件上面不能直接显示图片,需要创建一个可以显示图片控件
ImageView img = new ImageView(this);
// 先new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
// 最后在前面添加一个ImageSwitcher
img.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageResource(mThumbIds[mThumbIds.length / 2]);
return img;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:layout_alignParentTop="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/gallery" />
</RelativeLayout>
package com.itarchy.gridview;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class MygalleryAdapter extends BaseAdapter {
private Context mContext;
private Integer[] data;
private int mGalleryItemBackground;
public MygalleryAdapter(Context context, Integer[] data) {
this.mContext = context;
this.data = data;
// 获取自定义的属性值,res/values/attrs.xml
TypedArray attr = mContext
.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.gv_list_item, null);
// 创建一个绑定对象
mViewHolder = new ViewHolder();
// 初始化控件
mViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
mViewHolder.img.setBackgroundResource(mGalleryItemBackground);
// 绑定
convertView.setTag(mViewHolder);
} else {
// 从绑定对戏里重新获取一个View
mViewHolder = (ViewHolder) convertView.getTag();
}
// 赋值
mViewHolder.img.setImageResource(data[position]);
return convertView;
}
static class ViewHolder {
ImageView img;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
android:layout_centerVertical="true"/>
</RelativeLayout>
// 获取自定义的属性值,res/values/attrs.xml: