gpt4 book ai didi

android - ListView 图像仅在我滚动(并消失)时显示。解释它是如何工作的

转载 作者:行者123 更新时间:2023-11-29 16:24:50 25 4
gpt4 key购买 nike

我知道这个问题已在其他帖子中得到处理,但在阅读了几篇文章和教程后我不明白...

我有每个 ListView 项的 follogin rox.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
android:layout_height="50sp"
android:layout_width="50sp"
android:layout_alignParentLeft="true"
android:id="@+id/ImageIcon"/>

<TextView
android:text="@+id/TextView01"
android:id="@+id/TitleText"
android:textStyle="bold"
android:textSize="20sp"
android:layout_toRightOf="@id/ImageIcon"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:textColor="#333333"
/>

<TextView
android:text="@+id/TextView02"
android:id="@+id/DescriptionText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:background="#FFFFFF"
android:textColor="#333333"
android:layout_toRightOf="@id/ImageIcon"
android:layout_below="@id/TitleText"
android:layout_alignWithParentIfMissing="true"
/>

</RelativeLayout>

我的 ListViewAdapter 具有以下 getView(带有 ViewWrapper)

public View getView(int position, View convertView, ViewGroup parent)
{
View row=convertView;
ViewWrapper wrapper=null;
Activity activity=(Activity)getContext();

RssItem item=getItem(position);

if (row == null) {

LayoutInflater inflater=activity.getLayoutInflater();
row=inflater.inflate(R.layout.row,null);
wrapper=new ViewWrapper(row);
row.setTag(wrapper);
}
else
{
wrapper=(ViewWrapper)row.getTag();
}

wrapper.getTitle().setText(item.getTitle());
String cleaned=item.getDescription().replaceAll("\\<.*?\\>", "");
int Long=cleaned.length();
if (Long<=100)
{
wrapper.getDescription().setText(cleaned);
}
else wrapper.getDescription().setText(cleaned.substring(0, 50)+"...");

String laurl=item.getImageUrl();

if (laurl!="")
{
Drawable cachedImage = imageloader.loadDrawable(laurl);
wrapper.getImage().setImageDrawable(cachedImage);
}
else
{
wrapper.getImage().setImageResource(R.drawable.icon);
}

return row;

}

class ViewWrapper {

private View base;
private TextView title=null;
private TextView description=null;
private ImageView icono=null;

ViewWrapper (View base) {
this.base=base;
}

public TextView getTitle() {
if (title==null) {
title=(TextView)base.findViewById(R.id.TitleText);
}
return title;
}

public TextView getDescription() {
if (description==null) {
description=(TextView)base.findViewById(R.id.DescriptionText);
}
return description;
}

public ImageView getImage() {
if (icono==null) {
icono=(ImageView)base.findViewById(R.id.ImageIcon);
}
return icono;
}

}

我正在通过以下类(class)获取图像。

public class ImageThreadLoader  {


//GlobalCache
private HashMap<String, SoftReference<Drawable>> imagecache;

public ImageThreadLoader()
{
imagecache= new HashMap<String, SoftReference<Drawable>>();
}

public Drawable loadDrawable(final String imageurl) //,final ImageCallback imageCallback)
{
if (imagecache.containsKey(imageurl))
{
SoftReference<Drawable> softReference=imagecache.get(imageurl);
Drawable drawable=softReference.get();
if (drawable != null) {
return drawable;
}
}

new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(imageurl);
imagecache.put(imageurl, new SoftReference<Drawable>(drawable));
}
}.start();
return null;
}

public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Drawable.createFromStream(inputStream, "src");
}

}

现在 ListView 可以平滑滚动,但开始时没有加载任何图像。然后,如果我反复上下滚动,图像会出现,有时会消失。

我认为问题是我真的不明白事情是如何运作的..

提前致谢

最佳答案

我今天遇到了同样的问题。文档和 Pavandroid 发布的视频中没有解释的是,您需要设置某些属性的状态,而不依赖于布局文件中设置的这些状态的默认值。

例如,在我的应用程序中,图像根据该行的数据状态显示在一行中。在我的布局文件中,默认情况下通常会显示图像。当调用 getView 并且不显示图像时,我使用

使图像不可见
If (hideImage)
someImage.setVisibility(View.INVISIBLE);

如果 hideImage 为 false,则可以合理地假设图像会显示,但在滚动过程中它最终会消失。我不得不明确设置可见性,而不是依赖布局文件默认值:

If (hideImage)
someImage.setVisibility(View.INVISIBLE);
else
someImage.setVisibility(View.VISIBLE);

这解决了问题。

关于android - ListView 图像仅在我滚动(并消失)时显示。解释它是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5002361/

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