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
- Gallery(SimpleAdapter实现)
- 用SimpleAdapter实现ListView
- SimpleAdapter实现点击事件
- ListView实例(SimpleAdapter实现)
- HorizontalScrollView实现Gallery
- HorizontalScrollView实现Gallery
- 实现gallery无限循环
- gallery实现tab
- android--Gallery的实现
- Gallery 实现方法
- Android--Gallery实现切一屏
- RecyclerView实现Gallery
- gallery的自定义实现
- ViewPager实现Gallery效果
- 使用ViewPager实现gallery
- 重写SimpleAdapter实现自定义ListView
- Android入门:ListView(SimpleAdapter实现)
- Android入门:ListView(SimpleAdapter实现)
- CRT远程连接服务器字符输出乱码解决一例
- WEKA简介与资源汇总
- ZOJ-1117
- java中关于异常的理解
- golang 格式化输入输出
- Gallery(SimpleAdapter实现)
- SSH整合配置缓存
- iOS 消息推送原理及实现总结
- TableLayout
- iOS 7: 如何为iPhone 5S编译64位应用
- android JNI库实现reboot,recovery
- 安装驱动程序的API,SetupCopyOEMInfW 和 UpdateDriverForPlugAndPlayDevices
- Python笔记(一) 数字、动态类型、字符串
- Ubuntu ——虚拟机上网NAT