[Android]笔记16:网格视图(GridView)的功能与用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/david_520042/article/details/75051618

带预览的图片浏览器main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.dezai.gridviewtest.MainActivity">
<GridView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/grid01"
    android:horizontalSpacing="1pt"
    android:verticalSpacing="1pt"
    android:numColumns="4"
    android:gravity="center">
</GridView>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

cell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:paddingBottom="4dip" android:layout_width="fill_parent">
    <ImageView
        android:layout_height="50pt"
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true">
    </ImageView>
</RelativeLayout>

java

package com.dezai.gridviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    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<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=(ImageView) findViewById(R.id.imageView);
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,
                R.layout.cell,new String[]{"image"},new int[]{R.id.image1});
        grid=(GridView)findViewById(R.id.grid01);
        grid.setAdapter(simpleAdapter);
        //添加列表项被选中的监听器
        grid.setOnItemSelectedListener(new AdapterView.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 AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id)
            {
                //显示被单击的图片
                imageView.setImageResource(imageIds[position]);
            }

        });

    }
}

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页