Android之GridView

vickyjfr 2013-08-27

这次的Demo实现的是 通过BaseAdapter将图片和问题传给GridView ,通过点击GridView准确获取第几个

重点 1.如何将图片和文字通过BaseAdapter传给GridView

        2.如何通过点击GridView进行下一步操作


Android之GridView
 

package com.example.onclickgridview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
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.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
//	作用: 
//	1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 
//
//	2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素. 
    private LayoutInflater mInflater;
	//创建初始化数据源
	private String[] mad = {"1","2","3","4","5","6","7","8","9"};
	//声明控件
	private GridView mGridView;
	//声明adpter
	private Adapter mAdapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	//关联	
	mGridView = (GridView) findViewById(R.id.gridView1);	
	
	mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	
	//新建BaseAdapter
	mAdapter = new BaseAdapter() {
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text;
			ImageView imgview;
			RelativeLayout rLayout;
			//初始化时加载页面用到的 if里的东西
			 if (convertView == null) {
				 rLayout = (RelativeLayout) mInflater.inflate(R.layout.new_file, parent, false);

		        } else {
		        	rLayout = (RelativeLayout) convertView;
		        }
			String item = (String) getItem(position);
			//设置控件取回的数据
			text = (TextView) rLayout.findViewById(R.id.TextView2);

			text.setText(item);
			
			imgview = (ImageView) rLayout.findViewById(R.id.ImageView1);
			
			imgview.setImageResource(R.drawable.ic_launcher);
			
			
			return rLayout;
		}
		//本次重点 准确返回数据位置的关键 必须在getItemID设置对的位置选项默认从0开始
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mad[position];
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mad.length;
		}
	};
	
	mGridView.setAdapter((ListAdapter) mAdapter);
	//监听事件 对于按下gridView控件的其中一个做出的响应
	mGridView.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
			
			Toast.makeText(MainActivity.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();
			
		}
		
		
	});
	
	
	
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="21dp"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

 

相关推荐

sgafdsg / 0评论 2011-01-24