美文网首页Android 成长笔记
Android GridView 使用示例

Android GridView 使用示例

作者: 赵者也 | 来源:发表于2017-03-14 11:26 被阅读208次

GridView 常用的属性

XML 属性 相关的Java方法 说明
android:columnWidth setColumnWidth(int) 设置列的宽度
android:gravity setGravity(int) 设置对齐方式
android:horizontalSpacing setHorizontalSpacing(int) 设置各元素之间的水平间距
android:numColumns setNumColumns(int) 设置列数
android:stretchMode setStretchMode(int) 设置拉伸模式
android:verticalSpacing setVerticalSpacing(int) 设置各元素之间的垂直间距

页面布局文件:

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

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="1pt"
        android:verticalSpacing="1pt"
        android:numColumns="4"
        android:gravity="center"
        />

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

</LinearLayout>

主程序代码:

package com.toby.personal.testlistview;

import android.app.Activity;
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 Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int[] imageIds = new int[]{
                R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,
                R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
                R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12
        };

        List<Map<String, Object>> listItems = new ArrayList<>();

        for (int i = 0; i < imageIds.length; ++i) {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }

        GridView gridView = (GridView) findViewById(R.id.gridView);

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell,
                new String[] { "image" }, new int[] {R.id.cellImage});

        gridView.setAdapter(simpleAdapter);

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);

        gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                imageView.setImageResource(imageIds[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                imageView.setImageResource(imageIds[i]);
            }
        });

        imageView.setImageResource(imageIds[0]);

    }
}

主程序中用到的cell布局文件和cellImage对象,在cell.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cellImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    />

程序运行效果:


GridView 使用示例

参考文献:《疯狂Android讲义(第2版)》

相关文章

网友评论

    本文标题:Android GridView 使用示例

    本文链接:https://www.haomeiwen.com/subject/btmcnttx.html