2010.12.29(2)——— android GridView

2010.12.29(2)——— android 可伸缩的GridView

用 GridView 来实现九宫格布局 并且一个格显示一个图片和一行字

1、ImageAdapter

package com.huitu.shyj.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huitu.shyj.activity.R;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	
	public ImageAdapter(Context c)	{
		mContext=c;
	}
	
	public int getCount() {
		return mThumbIds.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout layoutView;
		
		if(convertView==null){
			layoutView=new LinearLayout(mContext);
			layoutView.setOrientation(LinearLayout.VERTICAL);			

			ImageView iconView;
			TextView iconText;
			
			iconView=new ImageView(mContext);
			iconView.setLayoutParams(new GridView.LayoutParams(60,60));
			iconView.setScaleType(ImageView.ScaleType.CENTER_CROP);		
			iconView.setImageResource(mThumbIds[position]);
			
			iconText=new TextView(mContext);
			iconText.setText(mThumbStrings[position]);
			
			LinearLayout.LayoutParams relParams=new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.FILL_PARENT
			);
			
			layoutView.addView(iconView, relParams);
			layoutView.addView(iconText, relParams);
			
			layoutView.setPadding(5, 10, 5, 10);
			layoutView.setGravity(Gravity.CENTER);
		}
		else {
			layoutView=(LinearLayout)convertView;
		}		
		
		return layoutView;
	}
	
	private Integer[] mThumbIds={
		R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3,
		R.drawable.icon_4,R.drawable.icon_5,R.drawable.icon_6,
		R.drawable.icon_7,R.drawable.icon_8,R.drawable.icon_9,
		R.drawable.icon_10,R.drawable.icon_11,R.drawable.icon_12,
		R.drawable.icon_13,R.drawable.icon_14,R.drawable.icon_15
	};
	
	private Integer[] mThumbStrings={
		R.string.app_act1,R.string.app_act2,R.string.app_act3,
		R.string.app_act4,R.string.app_act5,R.string.app_act6,
		R.string.app_act7,R.string.app_act8,R.string.app_act9,
		R.string.app_act10,R.string.app_act11,R.string.app_act12,
		R.string.app_act13,R.string.app_act14,R.string.app_act15
	};
}



2、MainActivity

package com.huitu.shyj.activity;

import java.io.File;

import com.huitu.shyj.activity.R;
import com.huitu.shyj.adapter.ImageAdapter;
import com.huitu.shyj.util.SHYJTools;

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

public class MainActivity extends Activity {
	public static final String ROOTPATH="/sdcard/SHYJ";
	public SharedPreferences settings;
	
	Intent intent;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        File file=new File(ROOTPATH);
        if(!file.exists()){
        	file.mkdir();
        }
        
        //未设置跳转至设置页面
        settings=getSharedPreferences(MXiTongSZ.SETTING_INFOS ,0);
        String strwebservice=settings.getString(MXiTongSZ.WEBSERVICE, "");
        String strlat=settings.getString(MXiTongSZ.LAT, "");
        String strlon=settings.getString(MXiTongSZ.LON, "");
        if(strwebservice.equals("")||strlat.equals("")||strlon.equals("")){
        	intent=new Intent();
        	intent.setClass(MainActivity.this, MXiTongSZ.class);
        	startActivity(intent);
        }
        
        SHYJTools.SHYJ_RECEIVE=settings.getInt(MXiTongSZ.RECEIVE, 0);
        
        setTitle(R.string.app_name);
        GridView gridView=(GridView)findViewById(R.id.main);
        gridView.setAdapter(new ImageAdapter(this));
        
        gridView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				switch(arg2){
				case 0:
					intent=new Intent();
					intent.setClass(MainActivity.this, MYuJingXX.class);
					startActivity(intent);
					break;
					
				case 1:
					intent=new Intent();
					intent.setClass(MainActivity.this, MYingJiXY.class);
					startActivity(intent);					
					break;
					
				case 2:
					intent=new Intent();
					intent.setClass(MainActivity.this, MYuJingCX.class);
					startActivity(intent);					
					break;
					
				case 3:
					intent=new Intent();
					intent.setClass(MainActivity.this, MYuQingJS.class);
					startActivity(intent);					
					break;

				case 4:
					intent=new Intent();
					intent.setClass(MainActivity.this, MShuiQingJS.class);
					startActivity(intent);					
					break;

				case 5:
					intent=new Intent();
					intent.setClass(MainActivity.this, MZhiBanXX.class);
					startActivity(intent);					
					break;
					
				case 6:
					intent=new Intent();
					intent.setClass(MainActivity.this, MYuAnCX.class);
					startActivity(intent);					
					break;

				case 7:
					intent=new Intent();
					intent.putExtra("AppType", "EPCX");
					intent.setClass(MainActivity.this, MQueryEvent.class);
					startActivity(intent);					
					break;

				case 8:
					intent=new Intent();
					intent.putExtra("AppType", "EVCX");
					intent.setClass(MainActivity.this, MQueryEvent.class);
					startActivity(intent);					
					break;

				case 9:
					intent=new Intent();
					intent.setClass(MainActivity.this, MTianQiYB.class);
					startActivity(intent);					
					break;

				case 10:
					intent=new Intent();
					intent.setClass(MainActivity.this, MPicActivity.class);
					intent.putExtra("AppType", "WXYT");
					startActivity(intent);					
					break;

				case 11:
					intent=new Intent();
					intent.setClass(MainActivity.this, MPicActivity.class);
					intent.putExtra("AppType", "TFLJ");
					startActivity(intent);					
					break;

				case 12:
					intent=new Intent();
					intent.setClass(MainActivity.this, MFangXunZZ.class);
					startActivity(intent);					
					break;

				case 13:
					intent=new Intent();
					intent.setClass(MainActivity.this, MXiTongSZ.class);
					startActivity(intent);					
					break;

				case 14:
					finish();
					break;

				default:
					finish();
				}
			}        	
        });
    }
}



另一种方法 不用自己写adapter
参考 http://blog.csdn.net/hellogv/archive/2009/09/18/4567095.aspx










你可能感兴趣的:(android,webservice,OS)