GridView

程式碼修行者發表於2015-07-21
package com.example.administrator.gridview;

import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

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


public class MainActivity extends Activity {
    private GridView gridView;
    private Context mContext;

    private List<Map<String,Object>> data_list;

    private Object[]pics = {R.drawable.address_book,R.drawable.calendar,R.drawable.camera,R.drawable.clock,R.drawable.games_control,R.drawable.messenger,R.drawable.ringtone,R.drawable.settings,R.drawable.speech_balloon,R.drawable.weather,R.drawable.world,R.drawable.youtube};
    private String[]texts = {"地址簿","日曆","照相機","時鐘","遊戲","信使","鈴聲","設定","微信","天氣","地圖","YouTube"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        gridView = (GridView)findViewById(R.id.gridView);

        data_list = new ArrayList<Map<String,Object>>();
        SimpleAdapter adapter = new SimpleAdapter(mContext,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String,Object> _item = (HashMap<String,Object>)gridView.getItemAtPosition(position);
                Toast.makeText(mContext,_item.get("pic") + "=>" + _item.get("text"),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private List<Map<String,Object>> getData(){
        int length = pics.length;
        for (int i=0  ; i < length ; i++){
            Map<String,Object> data = new HashMap<String, Object>();
            data.put("pic",pics[i]);
            data.put("text",texts[i]);
            data_list.add(data);
        }
        return data_list;
    }
}

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:gravity="center"
    >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/pic"
        android:src="@mipmap/ic_launcher"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="文字"
        android:textColor="#FFF"
        android:layout_marginTop="5dp"/>
</LinearLayout>

這裡寫圖片描述

相關文章