• android学习---gridview


    布局文件:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:orientation="horizontal" >
    
        <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:horizontalSpacing="10dp"
            android:numColumns="3"
            android:padding="20dp"
            android:verticalSpacing="10dp" >
        </GridView>
    
        <ImageView
            android:id="@+id/imageview"
            android:layout_width="fill_parent"
            android:layout_height="150dp" >
        </ImageView>
    
    </LinearLayout>

    新建xml文件,cell.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/imageview"
            android:layout_width="72dp"
            android:layout_height="72dp" >
        </ImageView>
    
    </LinearLayout>

    实现代码:

    package com.leaf.android;
    
    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.telephony.CellLocation;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.SimpleAdapter;
    
    public class Main extends Activity implements OnItemClickListener,
            OnItemSelectedListener {
    
        private ImageView imageView;
        private GridView gridView;
        private int[] resIds = new int[] { R.drawable.car1, R.drawable.car2,
                R.drawable.car3, R.drawable.car4, R.drawable.car5, R.drawable.car6,
                R.drawable.car7, R.drawable.car8, R.drawable.car9 };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            gridView = (GridView) this.findViewById(R.id.gridview);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < resIds.length; i++) {
                Map<String, Object> cell = new HashMap<String, Object>();
                cell.put("imageview", resIds[i]);
                list.add(cell);
            }
            SimpleAdapter simpleAdapter = new SimpleAdapter(Main.this, list,
                    R.layout.cell, new String[] { "imageview" },
                    new int[] { R.id.imageview });
            gridView.setAdapter(simpleAdapter);
            imageView = (ImageView) this.findViewById(R.id.imageview);
            gridView.setOnItemSelectedListener(this);
            gridView.setOnItemClickListener(this);
            imageView.setImageResource(resIds[0]);
    
        }
    
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            imageView.setImageResource(resIds[position]);
        }
    
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
    
        }
    
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            imageView.setImageResource(resIds[position]);
    
        }
    }

    效果:

  • 相关阅读:
    java基础(一)
    java概述
    七大查找十大排序之二排序
    bat批处理脚本语言(一)
    photoshop安装与破解
    office——excel常用函数
    arcgis engine开发环境搭建
    七大查找十大排序算法(一)
    华为路由交换常用命令
    cisco路由交换常用命令
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3298432.html
Copyright © 2020-2023  润新知