1、简介
GridView 以行列的方式来布局View,是一种常用的布局视图组件。
2、实现步骤
2.1 常见GridView布局文件
2.2 实例化
2.3 绑定数据
2.4 响应事件
3、代码演示
3.1 基本GridView
布局文件
<?xml version=”1.0″ encoding=”utf-8″?> <GridView xmlns:android=”https://schemas.android.com/apk/res/android” android:id=”@+id/gridview” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:columnWidth=”90dp” android:numColumns=”auto_fit” android:verticalSpacing=”10dp” android:horizontalSpacing=”10dp” android:stretchMode=”columnWidth” android:gravity=”center” />
程序代码
package com.amaker.gv; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private GridView gv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gv = (GridView) findViewById(R.id.gridview); // 绑定数据 ListView Spinner AutoCompleteTextView GridView gv.setAdapter(new ImageAdapter()); gv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, position+”–”, Toast.LENGTH_LONG).show(); } }); } class ImageAdapter extends BaseAdapter{ int[] images = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, }; @Override public int getCount() { return images.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(MainActivity.this); iv.setImageResource(images[position]); iv.setLayoutParams(new GridView.LayoutParams(85, 85)); iv.setScaleType(ImageView.ScaleType.CENTER_CROP); iv.setPadding(8, 8, 8, 8); /* iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, position+”", Toast.LENGTH_LONG).show(); } });*/ return iv; } } }
3.2 自定义GridView
布局文件
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”https://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > <ImageView android:id=”@+id/ImageView01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”/> <TextView android:text=”" android:id=”@+id/TextView01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”/> </LinearLayout>
程序代码
package com.amaker.gv; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity2 extends Activity { private GridView gv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gv = (GridView) findViewById(R.id.gridview); // 绑定数据 ListView Spinner AutoCompleteTextView GridView gv.setAdapter(new ImageAdapter()); gv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity2.this, position+”–”, Toast.LENGTH_LONG).show(); } }); } class ImageAdapter extends BaseAdapter{ LayoutInflater inflater; public ImageAdapter() { inflater = LayoutInflater.from(MainActivity2.this); } int[] images = { R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, }; String[] strs = { “Hello”, “Grid Demo”, “Hello”, “Grid Demo”, “Hello”, “Grid Demo”, }; @Override public int getCount() { return images.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View v = inflater.inflate(R.layout.gridview_item, null); ImageView iv = (ImageView) v.findViewById(R.id.ImageView01); TextView tv = (TextView)v.findViewById(R.id.TextView01); iv.setImageResource(images[position]); tv.setText(strs[position]); return v; } } }