多次运行的库getView方法上的自定义适配器

时间:2014-01-09 04:56:43

标签: android gallery custom-adapter

我已经制作了一个我正在展示图像的图库,我已经在图库中设置了自定义适配器。当我运行应用程序时,它运行正常,但自定义适配器的getView方法运行多次,因为它调用次数;但我只调用了一次。以下是我的代码。

gallery.setAdapter(new ImageAdapter(this));

上面一行设置了图库的自定义适配器。

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() 
    {
        return mImageIds.length;
    }

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position) 
    {
        tv.setText(names[position]);

        previousButton.setClickable(true);

        nextButton.setClickable(true);

        if (position == 0)
        {
            previousButton.setClickable(false);
        } 
        else if (position == 111)
        {
            nextButton.setClickable(false);
        }


        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

                     System.out.println("getview method::  "+ viewCheck++);

        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        if (width >= 720 && height >= 1280) 
        {
        imageView.setLayoutParams(new Gallery.LayoutParams(720, 680));
        } 
        else if (width >= 480 && height >= 800) 
        {
        imageView.setLayoutParams(new Gallery.LayoutParams(480, 440));
        }
        else if (width >= 320 && height >= 480) 
        {
        imageView.setLayoutParams(new Gallery.LayoutParams(320, 250));
        }

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}

0 个答案:

没有答案