0

目前我正在查看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

这里有两个问题:

  1. 为什么位置 0 的 ImageView 被创建了两次?

  2. 假设我想用 ImageViews 做点什么。我会创建 ImageView 数组,在 getView() 中初始化其元素,但我无法在 onCreate() 甚至 onResume() 中使用它,因为 ImageViews 还不存在!我该怎么办?

对不起我的英语不好。

4

1 回答 1

1

1:我假设您滚动列表以获取此日志(因为仅当视图可见时才会调用 getView),因此您很可能只是再次向上滚动,并且位置 0 处的视图从顶部滚动并即将到来再次从顶部...

2:取决于您为什么要将您的视图放在onCreate或中onResume。如果没有其他解决方案并且您必须拥有它们,您可以简单地自己创建所有视图,onCreate将它们保存在列表中,然后在 getView 中返回。我会尽量避免这种情况,因为 ListView 仅在存在 Visible 并重用它们的情况下才将视图实例化。你会在我描述的实现中失去这个特性,所以请告诉我们,为什么你想在之前拥有它getView

于 2012-05-23T09:33:55.570 回答