Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中GridView的使用方法

Android中GridView的使用方法

編輯:關於Android編程

布局文件activity_main.xml



    
    

gridview_item.xml

這個是一個item的單元格樣式的,有圖片和文字




    
    

MainActivity.java

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.widget.GridView;

public class MainActivity extends Activity {

    private GridView gridView;
    private GridViewAdapter adapter;

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

        // 設置適配器的圖片資源
        int[] imageId = new int[] { R.drawable.chat_tool_camera,
                R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
                R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
                R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
                R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
                R.drawable.chat_tool_voice, R.drawable.chat_tool_camera,
                R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
                R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
                R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
                R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
                R.drawable.chat_tool_voice };

        // 設置標題
        String[] title = new String[] { "相機", "定位", "畫筆", "視頻", "聲音", "相機",
                "定位", "畫筆", "視頻", "聲音", "相機", "定位", "畫筆", "視頻", "聲音", "相機",
                "定位", "畫筆", "視頻", "聲音" };
        List> listitem = new ArrayList>();

        // 將上述資源轉化為list集合
        for (int i = 0; i < title.length; i++) {
            Map map = new HashMap();
            map.put("image", imageId[i]);
            map.put("title", title[i]);

            listitem.add(map);
        }
        adapter = new GridViewAdapter(MainActivity.this, listitem);

        gridView = (GridView) this.findViewById(R.id.gridView);
        gridView.setAdapter(adapter);

    }
}

GridViewAdapter.java

這個是適配器

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List> listitem;

    public GridViewAdapter(Context context,List> listitem) {
        this.context = context;
        this.listitem = listitem;
    }

    @Override
    public int getCount() {
        return listitem.size();
    }

    @Override
    public Object getItem(int position) {
        return listitem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
        }

        ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
        TextView textView = (TextView) convertView.findViewById(R.id.textView);

        Map map = listitem.get(position);
        imageView.setImageResource((Integer) map.get("image"));
        textView.setText(map.get("title") + "");
        return convertView;
    }

}

效果圖如下:
這裡寫圖片描述

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved