谷歌android官方GridView教程

时间:2012-05-23 09:17:46

标签: android gridview baseadapter

目前我在查看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还不存在!我该怎么办?

  3. 抱歉我的英文。

1 个答案:

答案 0 :(得分:1)

1:我假设您在List中滚动以获取此Log(因为只有在View可见时才调用getView),因此您可能只是再次向上滚动,并且在pos 0处的View被滚动到顶部并再次来自顶部...

2:取决于您希望在onCreateonResume中显示观看的原因。如果没有其他解决方案并且您必须拥有它们,您可以在onCreate中自己创建所有视图,将它们保存在List中,然后在getView中返回它。我会尽量避免这种情况,因为如果有Visible并且重用它们,ListView只会实例化视图。你将在我描述的实现中放弃这个功能,所以请告诉我们,为什么你想在getView

之前使用它