總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月10日 星期二

圖片瀏覽 ~ GridView與Adapter的結合








GridViewActivity.java

package com.tsots.GridView_Adapter_Pic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class GridViewActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        //GridView的點選事件, 傳入int position知user點選哪一個item
        gridview.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView parent, View v, int position, long id) 
            {
                Toast.makeText(GridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}



我們預先將所需的圖檔放入/res/draeable/,命名pic_01.png~pic_34.png
由GridView.LayoutParams(110, 110)可以設定顯示圖片的寬高

ImageAdapter.java

package com.tsots.GridView_Adapter_Pic;

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

public class ImageAdapter extends BaseAdapter 
{
    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return mThumbIds.length;
    }

    public Object getItem(int position) 
    {
        return null;
    }

    public long getItemId(int position) 
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) 
        {  
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } 
        else 
        {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    private Integer[] mThumbIds = 
    {
            R.drawable.pic_01, R.drawable.pic_02,
            R.drawable.pic_03, R.drawable.pic_04,
            R.drawable.pic_05, R.drawable.pic_06,
            R.drawable.pic_07, R.drawable.pic_08,
            R.drawable.pic_09, R.drawable.pic_10,
            R.drawable.pic_11, R.drawable.pic_12,
            R.drawable.pic_13, R.drawable.pic_14,
            R.drawable.pic_15, R.drawable.pic_16,
            R.drawable.pic_17, R.drawable.pic_18,
            R.drawable.pic_19, R.drawable.pic_20,
            R.drawable.pic_21, R.drawable.pic_22,
            R.drawable.pic_23, R.drawable.pic_24,
            R.drawable.pic_25, R.drawable.pic_26,
            R.drawable.pic_27, R.drawable.pic_28,
            R.drawable.pic_29, R.drawable.pic_30,
            R.drawable.pic_31, R.drawable.pic_32,
            R.drawable.pic_33, R.drawable.pic_34
    };
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//使有底框色
gridview.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.paleyellow));
//會移動scroll跳到點選的那張圖為置頂
gridview.setSelection(position);
//目前畫面可見的GridView中子項有幾個
gv_img_mood.getChildCount();//18
//GridView中所有子項有幾個
gv_img_mood.getAdapter().getCount());//34

沒有留言:

張貼留言