目前我正在查看http://developer.android.com/resources/tutorials/views/hello-gridview.html 我在 onCreate()、onResume 和 getView() 中创建了带有附加日志的测试项目:
HelloGridViewActivity.java
package com.hello.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class HelloGridViewActivity extends Activity {
private static final String TAG = "HelloGridViewActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Log.e(TAG, "onCreate()");
}
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume()");
}
}
ImageAdapter.java
package com.hello.namespace;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private static final String TAG = "ImageAdapter";
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;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
Log.e(TAG, "position = " + position);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
日志输出:
05-23 08:59:13.541: E/HelloGridViewActivity(733): onCreate()
05-23 08:59:13.551: E/HelloGridViewActivity(733): onResume()
05-23 08:59:13.711: E/ImageAdapter(733): position = 0
05-23 08:59:14.380: E/ImageAdapter(733): position = 1
05-23 08:59:15.190: E/ImageAdapter(733): position = 2
05-23 08:59:15.771: E/ImageAdapter(733): position = 3
05-23 08:59:16.340: E/ImageAdapter(733): position = 4
05-23 08:59:17.141: E/ImageAdapter(733): position = 5
05-23 08:59:17.721: E/ImageAdapter(733): position = 6
05-23 08:59:18.330: E/ImageAdapter(733): position = 7
05-23 08:59:18.611: E/ImageAdapter(733): position = 8
05-23 08:59:18.631: E/ImageAdapter(733): position = 9
05-23 08:59:18.631: E/ImageAdapter(733): position = 10
05-23 08:59:18.651: E/ImageAdapter(733): position = 11
05-23 08:59:18.651: E/ImageAdapter(733): position = 12
05-23 08:59:18.671: E/ImageAdapter(733): position = 13
05-23 08:59:18.681: E/ImageAdapter(733): position = 14
05-23 08:59:18.691: E/ImageAdapter(733): position = 15
05-23 08:59:18.701: E/ImageAdapter(733): position = 16
05-23 08:59:18.721: E/ImageAdapter(733): position = 17
05-23 08:59:18.721: E/ImageAdapter(733): position = 18
05-23 08:59:18.741: E/ImageAdapter(733): position = 19
05-23 08:59:18.741: E/ImageAdapter(733): position = 20
05-23 08:59:18.891: E/ImageAdapter(733): position = 0
这里有两个问题:
为什么位置 0 的 ImageView 被创建了两次?
假设我想用 ImageViews 做点什么。我会创建 ImageView 数组,在 getView() 中初始化其元素,但我无法在 onCreate() 甚至 onResume() 中使用它,因为 ImageViews 还不存在!我该怎么办?
对不起我的英语不好。