安卓学习第14课——GridView(简单图片浏览器1)

时间:2023-01-13 14:45:31

今天学习的是GridView组件,利用的部分只是还是Adapter、imageView的知识,制作了简单图片浏览器

<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:gravity="center_horizontal"
    android:orientation="vertical" >

    <!-- 定义一个GridView组件 -->

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:horizontalSpacing="1pt"
        android:numColumns="4"
        android:verticalSpacing="1pt" />
    <!-- 定义一个imageView组件 -->

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:padding="2pt"
    >
<ImageView
    android:id="@+id/image1"
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    />    
</LinearLayout>
package com.example.gridview;


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.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    GridView grid;
    ImageView imageView;
int[] imageIds=new int[]{
    R.drawable.bomb5,R.drawable.bomb6,R.drawable.bomb7,R.drawable.bomb8,
    R.drawable.bomb9,R.drawable.bomb10,R.drawable.bomb11,R.drawable.bomb12,
    R.drawable.bomb13,R.drawable.bomb14,R.drawable.bomb15,R.drawable.bomb16
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个List对象,List对象元素师Map
        List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
        for(int i=0;i<imageIds.length;i++){
            Map<String,Object> listItem=new HashMap<String,Object>();
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }
        imageView=(ImageView) findViewById(R.id.imageView);
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});
        grid=(GridView) findViewById(R.id.gridView1);
        grid.setAdapter(simpleAdapter);
        grid.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                imageView.setImageResource(imageIds[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {    
            }
        });
        grid.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                imageView.setImageResource(imageIds[position]);
            }
        });
        
    }

}

安卓学习第14课——GridView(简单图片浏览器1)这个图片浏览器,确实很基本。只是利用事件监听,将gridView里面的图片显示在imageview里,进而显示出图片浏览器效果。