当我滚动(消失)的ListView图像仅表现。它是如何工作的说明 [英] ListView Images are only showed when I scroll (and disappear). Explanation of how it works
问题描述
我知道问题在其他职位,但读了几篇文章和教程,我不明白之后得到治疗......
I know that the problem have been treated in other post but after reading several articles and tutorial i don't understand...
我为每个ListView项的follogin rox.xml
I have the follogin rox.xml for each listview item
<?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)
My ListViewAdapter has the following getView (with a 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;
}
}
和我得到的图像throgh以下类。
And I'm getting images throgh the following 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的滚动顺畅,但一开始没有图像被加载。然后,如果我向上和向下滚动反复图像显示和某个消失。
Now the listview scrolls smoothly but at the beginning no image are loaded. Then if I scroll up and down repeatedly images appear and sometime disappear.
我认为问题是,我真的不明白的事情是如何工作的。
I think the problem is that i really dont understand how things works..
谢谢adavance
Thanks in adavance
推荐答案
可能要看看这个:
http://www.youtube.com/watch?v=wDBM6wVEO70
这是从谷歌I /会话Ø完全了解列表视图。
It's a session from Google I/O entirely about ListViews.
这篇关于当我滚动(消失)的ListView图像仅表现。它是如何工作的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!