码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-21 16:41:20      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:android学习笔记之gridview   gridview   android   适配器   


(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)运行效果如下:

技术分享



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

标签:android学习笔记之gridview   gridview   android   适配器   

原文地址:http://blog.csdn.net/xlgen157387/article/details/42060867

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!