Android—— GridView简单使用详解

效果图(页面图片可以点击查看大图):



项目结构:

代码实现(代码重要部分有注释,相信大家都能看懂):

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"
    tools:context="${relativePackage}.${activityClass}" >

    <GridView 
        android:id="@+id/grid1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" />
        
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView 
        android:id="@+id/image"
        android:layout_width="100sp"
        android:layout_height="150sp"
        android:scaleType="fitXY" />
	<TextView 
	    android:id="@+id/text"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center"/>
</LinearLayout>

MainActivity.java

package com.example.gridviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity implements OnItemClickListener {
	
	private GridView grid;
	//图片数组,图片id
	private int[] images = new int[]{R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4 ,R.drawable.p5,R.drawable.p6};
	//图片下面文字
	private String[] titles = new String[]{"MM1","MM2","MM3","MM4", "MM5", "MM6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        grid = (GridView) findViewById(R.id.grid1);
        SimpleAdapter adapter = getAdapter();
        grid.setAdapter(adapter);
        //图片点击
        grid.setOnItemClickListener(this);
    }

	private SimpleAdapter getAdapter() {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		//把数据放入list中
		for(int i = 0; i < 6; i++){
			HashMap<String, Object> m = new HashMap<String, Object>();
			m.put("image", images[i]);
			m.put("text", titles[i]);
			list.add(m);
		}
		String from[] = new String[]{"image", "text"};
		int[] to = new int[]{R.id.image, R.id.text};
		//适配器
		SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, from, to);
		return adapter;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//取出用户点击的当前元素
		HashMap<String, Object> m = (HashMap<String, Object>) parent.getItemAtPosition(position);
		//图片id
		Integer pid = (Integer) m.get("image");
		Intent intent = new Intent(this, ImageActivity.class);
		intent.putExtra("pid", pid);
		startActivity(intent);
	}
}

activity_image.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"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- android:scaleType="fitXY"等比拉伸 -->
    <ImageView 
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"/>

</LinearLayout>

ImageActivity.java

package com.example.gridviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class ImageActivity extends Activity {
	
	private ImageView image1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image);
		
		image1 = (ImageView) findViewById(R.id.image1);
		Intent intent = getIntent();
		//取出传递过来的id
		Bundle bundle = intent.getExtras();
		int pid = bundle.getInt("pid");
		image1.setImageResource(pid);
	}
}

这种方法比较简单,使用的是已有简单适配器 。
还可以个性化定制比较强,有自定义的适配器,还有自定义的layout 

gridview的应用思路就是这样,不需要刻意去记代码,要灵活运用 。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值