zoukankan      html  css  js  c++  java
  • 控件:Gallery 2.(SimpleAdapter)

    除了使用BaseAdapter之外,SimpleAdapter也是一种最为常见的适配器操作类。
    既然决定要使用SimpleAdapter类了,那么就需要为其准备一个List集合,而在这个List集合之中,一定要保存Map集合,而且Map集合的key必须是String。

    grid_layout.xml

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

    main.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@+id/MyLayout"
    android:orientation
    ="vertical"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent">
    <Gallery
    android:id="@+id/myGallery"
    android:gravity
    ="center_vertical"
    android:spacing
    ="3px"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"/>
    </LinearLayout>

    MyGalleryDemo.java

    View Code
    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.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Gallery;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;

    public class MyGalleryDemo extends Activity {
    // 图片浏览
    private Gallery gallery = null;
    private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;
    // 适配器
    private SimpleAdapter simpleAdapter = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 调用布局文件
    super.setContentView(R.layout.main);
    // 初始化适配器
    this.initAdapter() ;
    // 取得组件
    this.gallery = (Gallery) super.findViewById(R.id.myGallery) ;
    // 设置图片集
    this.gallery.setAdapter(this.simpleAdapter);
    // 设置单击事件
    this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;
    }
    private class OnItemClickListenerImpl implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // 显示图片编号
    Toast.makeText(MyGalleryDemo.this,
    "" + position, Toast.LENGTH_SHORT).show();
    }
    }

    // 初始化适配器
    public void initAdapter(){
    Field[] fields = R.drawable.class.getDeclaredFields();
    for (int x = 0; x < fields.length; x++) {
    // 所有ispic_*命名的图片
    if (fields[x].getName().startsWith("ispic_")){
    // 定义Map
    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) ; // 保存Map
    }
    }
    // 实例化SimpleAdapter
    this.simpleAdapter = new SimpleAdapter(
    this,
    this.list, // 要包装的数据集合
    R.layout.grid_layout, // 要使用的显示模板
    new String[] { "img" }, // 定义要显示的Map的Key
    new int[] {R.id.img }); // 与模板中的组件匹配
    }
    }
  • 相关阅读:
    MSP430G2553 4x4矩阵键盘
    数论 (1)
    吐血分享一款免费看所有付费影视的app,不好用来石欠我
    Nginx如何部署静态web项目
    SpringBoot-RestTemplate实现调用第三方API
    信息收集小技巧
    kali常用命令
    kali中的Firefox改中文
    反射与注解
    MyBatis-Plus 基本用法
  • 原文地址:https://www.cnblogs.com/androidsj/p/2379350.html
Copyright © 2011-2022 走看看