Android学习笔记之GridView的使用详解


(1)创建布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:numColumns="3" >

    </GridView>

</RelativeLayout>

(2)在MainActivity类中使用该布局

package com.example.layoutstudy;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private GridView gridView;
	private ImageAdapter adapter;

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

		gridView = (GridView) this.findViewById(R.id.gridView);
		adapter = new ImageAdapter();
		// 设置一个适配器
		gridView.setAdapter(adapter);
		/*
		 * 创建一个点击事件,当点击某一个图片的时候显示他的位置
		 */
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MainActivity.this, "-->>" + position, 1).show();
			}
		});
	}

	// 定义图片的资源
	private int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3,
			R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7,
			R.drawable.r8 };

	/*
	 * 适配器的定义,要继承BaseAdapter
	 */
	public class ImageAdapter extends BaseAdapter {

		public ImageAdapter() {
		}

		@Override
		public int getCount() {
			return images.length;
		}

		@Override
		public Object getItem(int position) {
			return images[position];
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			/*
			 * 1.手工创建对象 2.加载xml文件
			 */
			ImageView imageView = null;
			if (convertView == null) {
				imageView = new ImageView(MainActivity.this);
			} else {
				imageView = (ImageView) convertView;
			}
			// 设置GridView的显示的个子的间距
			imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(30, 20, 20, 20);
			imageView.setImageResource(images[position]);
			return imageView;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


(3)运行效果如下:

技术分享



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。