android 图片任意拖拉,android 拖拉图片 Gallery 用SimpleAdater实现

a4c26d1e5885305701be709a3d33442f.png

除了使用BaseAdapter之外,SimpleAdapter也是一种最为常见的适配器操作类。

既然要使用SimpleAdapter类,那么就需要准备一个List集合,而在这个List集合之中,

一定要保存Map集合,而且Map集合的key必须是String。

既然要设置适配器,那么就要把需要的所有图片资源都进行显示。现在只有少量的图片,如果现在的图片很多呢,例如100张,按照定义100个长度的数组并不可取。

在java中存在一种发射机制,此时可以利用发射机制进行动态加载。

如果需要使用SimpleAdater肯定需要定义一个内部的组件,这个是一个布局文件。

在main.xml中

encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/myGallery"

android:gravity="center_vertical"

android:spacing="3dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

在res/layout中新建grid_layout.xml

encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/img"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:layout_marginLeft="70dp"

android:scaleType="center"/>

在MyGalleryProjectDemo.java程序中

package com.tarena.galleryproject;

import java.text.DateFormat.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.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.Gallery;

import android.widget.SimpleAdapter;

import android.widget.Toast;

public class MyGalleryProjectDemo extends Activity {

private Gallery gallery = null;

private SimpleAdapter simpleAdapter =

null;

private

List

Integer>> list = new

ArrayList>();

@Override

public

void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.initAdapter();

this.initAdapter();

this.gallery = (Gallery) super.findViewById(R.id.myGallery);

this.gallery.setAdapter(this.simpleAdapter);

this.gallery.setOnItemClickListener(new

onItemClickListenerImpl());

}

private

void initAdapter(){

java.lang.reflect.Field[] fields =

R.drawable.class.getDeclaredFields(); //取得全部的属性

for(int x

= 0; x < fields.length; x++){

if(fields[x].getName().startsWith("ispic_")){ //我们所需要的图片,图片的头文件名为ispic_

Map map = new

HashMap();

try {

map.put("img", fields[x].getInt(R.drawable.class));

//必须定义好名称是img

} catch (Exception e) {

e.printStackTrace();

}

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 onItemClickListenerImpl implements OnItemClickListener{

public void

onItemClick(AdapterView> parent,

View view, int position,

long id) {

Toast.makeText(MyGalleryProjectDemo.this,

String.valueOf(position),

Toast.LENGTH_SHORT).show();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值