Gallery(SimpleAdapter实现)

来源:互联网 发布:python ide 编辑:程序博客网 时间:2024/05/21 19:45

MyDemo.javapackage com.jackie.simplegallery;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;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.ViewSwitcher.ViewFactory;public class MyDemo extends Activity {private ImageSwitcher myImageSwitcher = null;private Gallery gallery = null;private SimpleAdapter simpleAdapter = null;private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main);this.initAdapter();this.gallery = (Gallery) super.findViewById(R.id.gallery);this.myImageSwitcher = (ImageSwitcher) super.findViewById(R.id.myImageSwitcher);this.myImageSwitcher.setFactory(new ViewFactoryImpl());this.gallery.setAdapter(simpleAdapter);this.gallery.setOnItemClickListener(new OnItemClickListenerImpl());}    private class OnItemClickListenerImpl implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {   Map<String,Integer> map= (Map<String,Integer>)parent.getAdapter().getItem(position);   MyDemo.this.myImageSwitcher.setImageResource(map.get("img"));}        }private void initAdapter() {Field[] fields = R.drawable.class.getDeclaredFields(); // 取得全部的属性for (int x = 0; x < fields.length; x++) {if (fields[x].getName().startsWith("pic_")) { // 我们需要的图片Map<String, Integer> map = new HashMap<String, Integer>();try {map.put("img", fields[x].getInt(R.drawable.class));} catch (Exception e) {}this.list.add(map);}}this.simpleAdapter = new SimpleAdapter(this, this.list,R.layout.grid_layout, new String[] { "img" },new int[] { R.id.img });}private class ViewFactoryImpl implements ViewFactory {@Overridepublic View makeView() {ImageView imageView = new ImageView(MyDemo.this);imageView.setBackgroundColor(0xFFFFFFFF);imageView.setScaleType(ImageView.ScaleType.CENTER);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));return imageView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.my_demo, menu);return true;}}

main.xml<LinearLayout 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"    android:orientation="vertical"    android:gravity="bottom"    tools:context=".MyDemo" >  <ImageSwitcher         android:id="@+id/myImageSwitcher"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>     <Gallery        android:id="@+id/gallery"        android:spacing="3dp"        android:background="#00FFFFFF"        android:gravity="center_vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>grid_layout.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:scaleType="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>



0 0
原创粉丝点击