Android中用GridView实现九宫格的两种方法(转)

Android中用GridView实现九宫格的两种方法
http://blog.csdn.net/shakespeare001/article/details/7768455

 

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

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 gridView= (GridView) this.findViewById(R.id.gridView); gridView.setAdapter(new ImageAdapter(this)); gridView.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 GridHolder { 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) { GridHolder holder; if(convertView == null) { convertView = inflater.inflate(R.layout.gridview_item, null); holder = new GridHolder(); holder.phone_function_pic = (ImageView) convertView.findViewById(R.id.function_view); holder.phone_function_name = (TextView) convertView.findViewById(R.id.function_name); convertView.setTag(holder); } else { holder = (GridHolder) convertView.getTag(); } holder.phone_function_pic.setImageResource(mFunctionPics[position]); holder.phone_function_name.setText(mFunctionName[position]); return convertView; } } }

 

2.跟ListView类似的数据绑定方式(SimpleAdapter, ArrayList>)

http://blog.csdn.net/hellogv/article/details/4567095

你可能感兴趣的:(Android中用GridView实现九宫格的两种方法(转))