配置文件

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的屏幕上的title bar
  2. [Android] 环境配置之Android(安卓)Studio开发NDK
  3. Android常用资源总结
  4. andirons studio五大布局简介
  5. Android(安卓)Studio开发环境的搭建
  6. 线性布局控件间的布局设置
  7. [Android][设置最小(大)宽高]
  8. android实现TextView多行文本滚动
  9. Android第一行代码笔记

随机推荐

  1. Android(安卓)学习笔记(二) 各种技巧小知识
  2. 【Android(安卓)Studio】LinearLayout(线
  3. Android(安卓)8.1.0 源码修改之 - 屏蔽 H
  4. 史上最详细的Android原生APP中添加ReactN
  5. Android(安卓)中 Handler 引起的内存泄露
  6. android checkbox多选框实现
  7. chromium判断是否支持硬件加速的方法
  8. Android(安卓)ffmpeg详解
  9. Android中自定义属性(attrs.xml,TypedArray
  10. Android(安卓)View的小结