ch010 Android GridView
--------------------------------------------Layout activity_main.xml--------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="5" >
</GridView>
</LinearLayout>
--------------------------------------------Layout grid_items.xml-----------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
--------------------------------------------MainActivity.java-----------------------------------
package com.ch10;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
/**
*
* 项目名称:com.ch10
* 类名称:MainActivity
* 类描述: GridView、自定义适配器
* 创建人:方勇
* 创建时间:2012-11-15 下午2:38:16
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends Activity {
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setAdapter();
}
/* 初始化UI */
private void findViews() {
gridView = (GridView) findViewById(R.id.gv);
}
/* 设置GridView适配器 */
private void setAdapter() {
gridView.setAdapter(new MyAdapter(this));
}
}
--------------------------------------------MyAdapter.java------------------------------------
package com.ch10;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
*
* 项目名称:com.ch10
* 类名称:MyAdapter
* 类描述: 自定义适配器
* 创建人:方勇
* 创建时间:2012-11-15 下午2:34:03
* Copyright (c) 方勇-版权所有
*/
public class MyAdapter extends BaseAdapter {
/* 上下文 */
private Context mcontext;
private LayoutInflater layoutInflater;
/* 图像ID数组 */
private Integer[] images = { R.drawable.flag000, R.drawable.flag001, R.drawable.flag002, R.drawable.flag003,
R.drawable.flag004, R.drawable.flag005, R.drawable.flag006, R.drawable.flag007, R.drawable.flag008,
R.drawable.flag009, R.drawable.flag010, R.drawable.flag011, R.drawable.flag012, R.drawable.flag013,
R.drawable.flag014, R.drawable.flag015, R.drawable.flag016, R.drawable.flag017, R.drawable.flag018,
R.drawable.flag019, R.drawable.flag020, R.drawable.flag021, R.drawable.flag022, R.drawable.flag023,
R.drawable.flag024, R.drawable.flag025, R.drawable.flag026, R.drawable.flag027, R.drawable.flag028,
R.drawable.flag029, R.drawable.flag030, R.drawable.flag031, R.drawable.flag032, R.drawable.flag033,
R.drawable.flag034, R.drawable.flag035, R.drawable.flag036, R.drawable.flag037, R.drawable.flag038,
R.drawable.flag039, R.drawable.flag040, R.drawable.flag041, R.drawable.flag042, R.drawable.flag043,
R.drawable.flag044, R.drawable.flag045, R.drawable.flag046, R.drawable.flag047, R.drawable.flag048,
R.drawable.flag049, R.drawable.flag050, R.drawable.flag051, R.drawable.flag052, R.drawable.flag053,
R.drawable.flag054, R.drawable.flag055, R.drawable.flag056 };
public MyAdapter(Context context) {
super();
this.mcontext = context;
layoutInflater = LayoutInflater.from(mcontext);
}
/* Item个数,不设置这个getView方法无法执行 */
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
/**
* Item视图
*
* position 所在的位置
* convertView ImageView对象,第一屏时为空需实例化ImageView对象
* parent 所属视图组,默认一般设置为NULL
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = null;
/* 第一屏时,初始化所有的ImageView对象 */
if (null == convertView) {
/* 获取列表项视图,这里是一个相对布局 */
View itemView = layoutInflater.inflate(R.layout.grid_items, null);
/* 实例化所有的ImageView */
image = (ImageView) itemView.findViewById(R.id.image);
} else {// 滚动GridView时触发
image = (ImageView) convertView;
}
/* 指定图片 */
image.setImageResource(images[position]);
/* 设置图片大小 */
image.setLayoutParams(new GridView.LayoutParams(40, 40));
/** Options for scaling the bounds of an image to the bounds of this view. */
image.setScaleType(ImageView.ScaleType.FIT_XY);
return image;
}
}
--------------------------------------------效果图 GridView------------------------------------
推荐阅读
-
Android开发实现简单的观察者与被观察者示例
-
Android开发实现Launcher3应用列表修改透明背景的方法
-
android 中viewpager+fragment仿微信底部TAG完美渐变
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android 在viewPager中双指缩放图片双击缩放图片单指拖拽图片的实现思路
-
Android Studio 引入 aidl 文件的方法汇总
-
Android sdutio配置Zxing进行扫码功能的实现方法
-
详解Android Studio 3.0的新特性与适配
-
Android仿微信底部菜单栏效果
-
android随机生成圆形云标签效果