当前位置: 代码迷 >> Android >> Android顶用GridView实现九宫格的两种方法
  详细解决方案

Android顶用GridView实现九宫格的两种方法

热度:107   发布时间:2016-05-01 17:47:46.0
Android中用GridView实现九宫格的两种方法

1.传统办法:实现一个继承BaseAdapter的 ImageAdapter


java代码如下:


package com.test;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class GridViewActivity extends Activity {	//存放各功能图片	private Integer[] mFunctionPics = {R.drawable.mobilesecurity,R.drawable.infosecurity,			R.drawable.no_interrupt,R.drawable.bianjiejieting,R.drawable.xiaomishu			,R.drawable.sms_send_attime,R.drawable.yule,R.drawable.setting};	//存放各功能的名称	private Integer[] mFunctionName = {R.string.phone_tools_box_name1,			R.string.phone_tools_box_name2,R.string.phone_tools_box_name3,			R.string.phone_tools_box_name4,R.string.phone_tools_box_name5,			R.string.phone_tools_box_name6,R.string.phone_tools_box_name7,			R.string.phone_tools_box_name8};	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.gridview_main);				GridView girdview = (GridView) this.findViewById(R.id.girdview);		girdview.setAdapter(new ImageAdapter(this));		girdview.setOnItemClickListener(new OnItemClickListener() {			@Override			public void onItemClick(AdapterView<?> parent, View view,					int position, long id) {				Toast.makeText(GridViewActivity.this, "图片ID:"+mFunctionPics[position],1).show();			}		});			}		public class ImageAdapter extends BaseAdapter{		private Context mContext;		private LayoutInflater inflater;		private class GirdTemp{			ImageView phone_function_pic;			TextView phone_function_name;		}		public ImageAdapter(Context c){			mContext = c;			inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		}		@Override		public int getCount() {			return mFunctionPics.length;		}		@Override		public Object getItem(int position) {			return position;		}		@Override		public long getItemId(int position) {			return position;		}		@Override		public View getView(int position, View convertView, ViewGroup parent) {			GirdTemp temp;			if(convertView == null){				convertView = inflater.inflate(R.layout.gridview_item, null);				temp = new GirdTemp();				temp.phone_function_pic = (ImageView) convertView.findViewById(R.id.function_view);				temp.phone_function_name = (TextView) convertView.findViewById(R.id.function_name);				convertView.setTag(holder);			}else{				temp = (GirdHolder) convertView.getTag();			}			temp.phone_function_pic.setImageResource(mFunctionPics[position]);			temp.phone_function_name.setText(mFunctionName[position]);			return convertView;		}			}}

girdview_main.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"  
  6.     android:numColumns="auto_fit"  //自动适应多少行
  7.     android:verticalSpacing="20dp"  //两行之间的距离
  8.     android:horizontalSpacing="20dp"  //两列之间的距离
  9.     android:gravity="center"  
  10. /> 

girdview_item.xml源码:




  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout   
  3.         xmlns:android="http://schemas.android.com/apk/res/android"   
  4.  android:layout_height="wrap_content" 
  5. android:layout_width="fill_parent" 
  6.          <ImageView>   
  7.                 android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/function_view"
  10. android:layout_centerHorizontal="true"
  11. </ImageView> 
  12.          <TextView   
  13.                android:layout_width="wrap_content"   
  14.                android:layout_below="@+id/function_view"   
  15.                android:layout_height="wrap_content"   
  16.                android:layout_centerHorizontal="true"   
  17.                android:id="@+id/function_name">  
  18.          </TextView>  
  19. </RelativeLayout>  

 2.跟ListView类似的数据绑定方式

 

具体查看hellogv的这篇博客:http://blog.csdn.net/hellogv/article/details/4567095


  相关解决方案