配置文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:gravity="center_horizontal"
android:spacing="3px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

img_data.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"/>
</LinearLayout>

程序文件

MainActivity.java

package com.example.simpleadaptergalleryproject;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
private Gallery gallery=null;
private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();
private SimpleAdapter simpleAdapter=null;
private ImageSwitcher imageSwitcher=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.gallery=(Gallery)super.findViewById(R.id.gallery);//取得资源ID
this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher);//取得资源ID
this.imageSwitcher.setFactory(new ViewFactorylmpl());//设置转换工厂
this.initAdapter();//初始化适配器
this.gallery.setAdapter(this.simpleAdapter); //设置图片集
this.gallery.setOnItemClickListener(new OnItemClickListenerlmpl());//设置事件
}

private class ViewFactorylmpl implements ViewFactory{
@Override
public View makeView() {
ImageView image=new ImageView(MainActivity.this);
image.setBackgroundColor(0xFFFFFFFF);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return image;
}
}

private class OnItemClickListenerlmpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Toast.makeText(MainActivity.this,"一直都很帅,从未被超越",Toast.LENGTH_LONG).show();
Map<String,Integer> map=(Map<String,Integer>)MainActivity.this.simpleAdapter.getItem(arg2);
MainActivity.this.imageSwitcher.setImageResource(map.get("img"));
}
}

private void initAdapter(){//初始化适配器
Field[] field=R.drawable.class.getDeclaredFields();//取得全部属性
for (int i = 0; i < field.length; i++) {
if(field[i].getName().startsWith("ispic_")){ //找到所有以ispic_命名的图片
Map<String,Integer> map=new HashMap<String,Integer>();
try{
map.put("img", field[i].getInt(R.drawable.class)); //设置图片资源
}catch(Exception e){
}
this.list.add(map); //保存图片资源
}
}
this.simpleAdapter=new SimpleAdapter(MainActivity.this,
MainActivity.this.list,//要包装的数据集合
R.layout.img_data, //要使用的资源模板
new String[]{"img"},//要显示map中的Key
new int[]{R.id.img});//与模板中的组建向匹配
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

更多相关文章

  1. android开发资源合集
  2. android开发必看资源URL
  3. Android 网格视图(GirdView)简易适配器的使用
  4. android系统本身的图片资源
  5. Android回顾--(九) ListView和自定义适配器
  6. android 根据string 获取资源文件的id

随机推荐

  1. android日历之滚动选择日期类似ios
  2. Android菜鸟的成长笔记(4)——你真的理解了
  3. Android ADB 源码分析总结
  4. adb shell命令大全
  5. EditText 不显示光标 光标消失
  6. Android修炼之道——Shape
  7. android基于dialog加载时转圈圈的demo
  8. Android(安卓)视频分离和合成(MediaMuxer
  9. 通过AndroidTestCase来进行android 单元
  10. android琐碎记四