gpt4 book ai didi

android - Android 上的 GridView 滚动问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:37 25 4
gpt4 key购买 nike

这一定是我忽略的非常简单的事情,但我有以下问题(帖子相当长,但我想提供尽可能多的信息:))。

我的 Android 应用程序中有一个 gridview,其中每个单元格都包含自定义 View :

<?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 个图像和下一行的几个像素。但是当我开始滚动 gridvew 时,图像开始随机出现,即在最后一张图像之后我从一开始就很少看到,依此类推。出于好奇,我尝试了几个这样的样本:http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html...并看到相同的结果。

那么,我做错了什么吗?为什么 GridView 会显示比预期更多的项目?为什么项目出现在错误的位置?

BR,亚历克斯

最佳答案

实际上这里的问题是您在 if 或 else 中设置了“convertView”的内容。或者,如果 View 为 null,您应该始终在实例化 View 之后执行此操作,并且仅在返回 View 之前设置内容。

因此,您确定 View 的内容始终是正确的,使用位置更新而不是错误的回收 View 。

所以一般来说,您应该执行以下操作:
(基于 Android 指南 here 中的教程)

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
//just creating the view if not already present
} else {
imageView = (ImageView) convertView;
//re-using if already here
}

//here is the tricky part : set the content of the view out of the if, else
//just before returning the view
imageView.setImageResource(mThumbIds[position]);
return imageView;
}

关于android - Android 上的 GridView 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5369270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com