0

アプリに GridView があり、ほとんどすべてのデバイスで非常に優れていますが、... 何人かの友人をテストするためにそれを提供したところ、GridView でアプリがクラッシュすると言われました.アプリがクラッシュするか、実際にスクロールする必要があります気をつけて。一部の友人は、GridView は問題ないと言いました (私の HTC Wildfire、Samsung Galaxy Mini も) が、Samsung Galaxy S3、S4 を持っている友人はクラッシュすると言っています。私のGridView:

public static List<Item> items = new ArrayList<Item>();

...

GridView gridView = (GridView) findViewById(R.id.gridview);
        items.clear();
//add all items to list

    gridView.setAdapter(new ImageAdapter(this, items));
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
                    Intent i = new Intent(getApplicationContext(), SecondActivity.class);
                    i.putExtra("id", position);
                    startActivity(i);

                }
            });

イメージアダプター:

public class ImageAdapter extends BaseAdapter {

    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    public ImageAdapter(Context context, List<Item> items) {
        inflater = LayoutInflater.from(context);
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return items.get(i).drawable;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;
        TextView name;

        if (v == null) {
            v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.text, v.findViewById(R.id.text));
        }

        picture = (ImageView) v.getTag(R.id.picture);
        name = (TextView) v.getTag(R.id.text);

        Item item = (Item) items.get(i);

        picture.setImageResource(item.getDrawable());
        name.setText(item.name);

        return v;

    }

}

私の GridView の要素を 2 列にサイズ変更し、複数の画面で見栄えを良くする私の SquareImageView クラス:

public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }

}
4

1 に答える 1

2

setImageResource()のドキュメントから。(強調は私のものです)

これにより、 UI スレッドでビットマップの読み取りとデコードが行われるため、遅延が発生する可能性があります。それが懸念される場合は、代わりに setImageDrawable(android.graphics.drawable.Drawable) または setImageBitmap(android.graphics.Bitmap) と BitmapFactory の使用を検討してください。

画像の表示にはPicassoライブラリを使用することを強くお勧めします。ライブラリをプロジェクトに追加した後、次の関数を呼び出しますgetView()

Picasso.with(context).load(item.getDrawable()).into(picture);

以上です。


public class ImageAdapter extends BaseAdapter {

    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    private Context context; 

    public ImageAdapter(Context context, List<Item> items) {
        inflater = LayoutInflater.from(context);
        this.items = items;
        this.context = context;
    }
    .
    .
    .
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;
        TextView name;

        if (v == null) {
            v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.text, v.findViewById(R.id.text));
        }

        picture = (ImageView) v.getTag(R.id.picture);
        name = (TextView) v.getTag(R.id.text);

        Item item = (Item) items.get(i);

        Picasso.with(context).load(item.getDrawable()).into(picture);
        name.setText(item.name);

        return v; 
    }
}
于 2013-12-02T15:53:11.617 に答える