我在我的Android应用程序中有一个网格视图,每个单元格都包含自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id = "@+id/photosGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:drawSelectorOnTop="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:numColumns="6"
android:columnWidth="90dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
>
</GridView>
</RelativeLayout>
每个单元格都是
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.widgets.ImageThumbView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@android:color/transparent"
android:paddingLeft = "1dip"
android:paddingRight = "1dip"
android:paddingTop = "2dip"
android:paddingBottom = "2dip"
>
<ImageView
android:id="@+id/thumbImage"
android:layout_width="fill_parent"
android:layout_height = "fill_parent"
android:src="@drawable/icon_small"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height = "fill_parent"
android:background = "@android:color/transparent"
>
<ImageView
android:id="@+id/iconRight"
android:layout_width="40px"
android:layout_height = "40px"
android:src="@drawable/album_check"
android:visibility="gone"
android:layout_alignParentTop = "true"
android:layout_alignParentRight = "true"
/>
<ImageView
android:id="@+id/iconLeft"
android:src="@drawable/album_check"
android:visibility="gone"
android:layout_width = "40px"
android:layout_height="40px"
android:layout_alignParentTop = "true"
android:layout_alignParentLeft = "true"
/>
</RelativeLayout>
</com.myapp.widgets.ImageThumbView>
我的适配器长这样:
public class ImageAdapter extends BaseAdapter {
private List<String> mPictures = null;
public ImageAdapter(List<String> pictures) {
mPictures = pictures;
}
public int getCount() {
return mPictures != null ? mPictures.size() : 0;
}
public Object getItem(int position) {
return mPictures != null ? mPictures.get(position) : null;
}
public long getItemId(int position) {
return mPictures != null ? position : -1;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
ImageThumbView i = null;
try
{
Thread.sleep(100);
if (convertView == null)
{
String path = mPictures.get(position);
Log.d(((Integer)position).toString(), path);
i = addSingleView(_li, path);
TextView idx = (TextView) i.findViewById(R.id.caption);
if (idx != null)
idx.setText(((Integer)position).toString());
}
else
{
Log.d(((Integer)position).toString(), "ALREADY NOT NULL");
i = (ImageThumbView) convertView;
// These 2 lines were added only in desperate attempt to get it working, but it makes no difference
String path = mPictures.get(position);
i.updateView(path);
}
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
return i;
}
}
最初,它能正常工作,即显示前18个图像和下一行的几个像素。但是当我开始滚动GridView时,图像开始随机出现,即在最后一个图像之后,我看到了几个来自开头等等。出于好奇,我尝试了几个类似于这个样例: http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html,结果也是一样的。
那么,我做错了什么?为什么GridView会显示更多的项?为什么项目出现在错误的位置?
敬礼, Alex