我在我的应用程序中为一个库活动创建了一个GridView,并且我在活动首次启动时发现了奇怪的行为。我附上了一个打印声明,告诉我每个图像何时加载,这是我得到的输出:
01-30 17:27:09.597 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_07_146 into slot 1
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_34_369 into slot 2
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_38_530 into slot 3
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_45_214 into slot 4
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
似乎无论将多少项放入GridView,项目0都会多次加载,通常在15-25次之间。为什么要继续重装第一件商品?这是GridView的xml:
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:id="@+id/gridView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_centerHorizontal="true"
android:gravity="center" />
对于网格项目:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.thomas.galleryapp.Views.SquareImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/thumbnail"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/selectedCheckbox"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="10dp" />
</RelativeLayout>
我的自定义SquareImageView类:
public class SquareImageView extends ImageView {
public SquareImageView(final Context context)
{
super(context);
}
public SquareImageView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
Grid的图像适配器:
private class ImageAdapter extends BaseAdapter{
private LayoutInflater galleryItemInflater;
public ImageAdapter() {
galleryItemInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;
if (convertView == null) {
convertView = galleryItemInflater.inflate(R.layout.gallery_item, null);
}
imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
if (fileList.size() > 0){
imageView.setImageBitmap(fileList.get(position).getImage());
System.out.println("Loaded " + fileList.get(position).getFileName() + " into slot " + position);
}
return convertView;
}
public int getCount(){
return fileList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}