Android 에서 GridView 로 구 궁 격 을 실현 하 는 두 가지 방법 (회전)

Android 에서 GridView 로 구 궁 격 을 실현 하 는 두 가지 방법http://blog.csdn.net/shakespeare001/article/details/7768455
 
1. 전통 적 인 방법: BaseAdapter 를 계승 하 는 이미지 Adapter 실현
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 와 유사 한 데이터 바 인 딩 방식 (Simple Adapter, Array List >)
http://blog.csdn.net/hellogv/article/details/4567095

좋은 웹페이지 즐겨찾기