1. MainActivity.java
package home.lee.listviewadapterdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
public class MainActivity extends Activity {
//声明UI组件和资源变量等
private Gallery gallery;
private MyBaseAdapter adapter;
private ImageView imageView;
protected int[] imgs = {android.R.drawable.alert_dark_frame, android.R.drawable.alert_light_frame,
android.R.drawable.arrow_down_float, android.R.drawable.arrow_up_float,
android.R.drawable.bottom_bar, android.R.drawable.btn_default};;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setView();
}
private void setView() {
//初始化UI组件
imageView = (ImageView)findViewById(R.id.imageView1);
gallery = (Gallery)findViewById(R.id.gallery1);
adapter = new MyBaseAdapter(this);
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
imageView.setImageResource(imgs[arg2]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 2, R.string.exit);
menu.add(0, 2, 1, R.string.about);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
finish();
break;
case 2:
//Activity about content
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
2. MyBaseAdapter.java
package home.lee.listviewadapterdemo;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class MyBaseAdapter extends BaseAdapter {
//自定义图片Adapter以外部类形式存在于MainActivity中,也可以内部类形式以便访问MainActivity中的各个变量,特别是imgs数组
private static final String TAG = "Ray BaseAdapter";
private Context context;
private ImageView imageView;
private int[] imgs = {android.R.drawable.alert_dark_frame, android.R.drawable.alert_light_frame,
android.R.drawable.arrow_down_float, android.R.drawable.arrow_up_float,
android.R.drawable.bottom_bar, android.R.drawable.btn_default};
public MyBaseAdapter(Context context) {
super();
this.context = context;
}
@Override
public int getCount() {
return imgs.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//针对每一个数据(即每一个图片ID)创建一个ImageView实例,
imageView = new ImageView(context);//针对外面传递过来的Context变量
imageView.setImageResource(imgs[position]);//根据position获得图片资源
Log.i(TAG, "" + String.valueOf(imgs[position]));
imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));//设置Gallery中每幅图的大小
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
}
运行结果:
其改进应用可参考: 循环图库