码迷,mamicode.com
首页 > 其他好文 > 详细

GridView和SimpleAdapter实现网格布局

时间:2014-07-18 14:12:50      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

bubuko.com,布布扣

android:horizontalSpacing 元素之间的水平间距
android:verticalSpacing     元素之间的垂直间距
android:numColumns         设置列数
android:stretchMode          拉伸模式

该程序的目的是将图片用适配器放入4列的gridView中,点击一个图片下方出现预览图。

package com.kale.gridview02;

import java.util.ArrayList;
import java.util.HashMap;
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.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    GridView gV;
    ImageView iV;
    //将图片Id放入数组中去
    int []imageIds = new int[] {
            R.drawable.itunes,R.drawable.appstore,
            R.drawable.calculator,R.drawable.camera,R.drawable.mail,
            R.drawable.setting,R.drawable.safair
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        viewInit();
        //定义一个list来存放多个item元素
        ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
        for (int i = 0; i < imageIds.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("image", imageIds[i]);
            listItems.add(item);
        }
        //创建simpleAdapter来设置元素。1.context对象 2.list对象(数据源)3.item的布局文件 4.元素中的键名 5.该键名对应的组件的id
        SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.item,
                new String[] {"image"}, new int[] {R.id.item_imageView_id});
        gV.setAdapter(adapter);
        gV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                //点击哪个图片,下面的imageview就显示哪个
                iV.setImageResource(imageIds[position]);
                
            }
        });
    }
    
    private void viewInit() {
        gV = (GridView)findViewById(R.id.gridView_id);
        iV = (ImageView)findViewById(R.id.imageView_id);
    }
}
<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- 
        android:horizontalSpacing 元素之间的水平间距
        android:verticalSpacing   元素之间的垂直间距
        android:numColumns        设置列数
        android:stretchMode       拉伸模式
     -->
    <GridView
        android:id="@+id/gridView_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp"
        android:numColumns="4" 
        android:gravity="center">
    </GridView>

    <ImageView
        android:id="@+id/imageView_id"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_below="@+id/gridView_id"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView_id"
        android:layout_marginLeft="23dp"
        android:textSize="20sp"
        android:text="预览图片" />

</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/item_imageView_id"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

GridView和SimpleAdapter实现网格布局,布布扣,bubuko.com

GridView和SimpleAdapter实现网格布局

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/tianzhijiexian/p/3852824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!