Android GridView的使用

网格视图GridView的使用:

package com.Aina.Android;

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

public class ImageAdapter extends BaseAdapter {

	private Context mContext;
	private Integer[] imageIds = {
			R.drawable.img1,
			R.drawable.img2,
			R.drawable.img3,
			R.drawable.img4,
			R.drawable.img5,
			R.drawable.img6,
			R.drawable.img7,
			R.drawable.img8
	};
	public ImageAdapter(Context c){
		Log.i("TAG", "ImageAdapter()");
		this.mContext = c;
	}
	@Override
	public int getCount() {
		Log.i("TAG","getCount()");
		return imageIds.length;
	}

	@Override
	public Object getItem(int position) {
		Log.i("TAG","getItem()");
		return position;
	}

	@Override
	public long getItemId(int position) {
		Log.i("TAG","getItemId()");
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("TAG", position+"");
		ImageView iView = new ImageView(mContext);
		iView.setImageResource(imageIds[position]);
		return iView;
	}

}



<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/GridView"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:numColumns="auto_fit"
	android:verticalSpacing="20dp"
	android:horizontalSpacing="20dp"
	android:columnWidth="90dp"
	android:stretchMode="columnWidth"
	android:gravity="center">
</GridView>



package com.Aina.Android;

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

public class Test_GridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gView = (GridView) this.findViewById(R.id.GridView);
        gView.setAdapter(new ImageAdapter(this));//添加元素
        gView.setBackgroundResource(R.drawable.bg0);//添加背景
        gView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(Test_GridView.this, "选择了"+(arg2+1)+"号图片", Toast.LENGTH_SHORT).show();
			}
        	
        });
    }
}

你可能感兴趣的:(C++,c,android,OS,C#)