ListView和可变高度的项目导致的滚动问题 [英] ListView and variable height items causes scroll problems

查看:252
本文介绍了ListView和可变高度的项目导致的滚动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,我用它来滚动领域,文本和照片的清单。 这两个高度都是动态:TextView的可以包含一个很长的段落或只是一个句子,对图像进行缩放以适应宽度,这样的高度是可变的(你可以看到照片)。有些项目甚至没有图像。 这使得物品非常不均匀的高度。有一个项目,可以是2个屏幕长,而另一个只有一行文字。

I have a ListView that I use to scroll a list of fields, a text and a photo. Both heights are dynamic: The textview can contain a very long paragraph or just a sentence, and the image is scaled to fit width, so the height is variable (you can see the photo). Some items even don't have an image. This makes the items very heterogeneous in height. There is one item that can be 2 screens long, while another just one line of text.

我有工作等。但时,我向上滚动,因为回收的观点被插入在顶部,滚动位置跳到约新子的大小。向下滚动运行完美...

据我了解更多或更少的原因,显然是回收意见的大小完全不同,这使得一些滚动algorythm失败,无论它可能是。

I understand more or less why, obviously the recycled views are totally different in size, and this makes some scrolling algorythm fail, wherever it may be.

我看了一下覆盖 ITEMTYPE() itemTypeCount ,但我不知道它是否涉及到我的问题。在测试中,我做到了没有(神奇)的工作。

I have read about overriding itemType() and itemTypeCount, but I don't know if it's related to my problem. In the tests I did it didn't (magically) worked.

可以这样轻松地在这里和那里不动,任何伎俩还是魔法requestLayout?

Can this be fixed easily, any trick or magic requestLayout here and there?

有一个人建议写一个视图回收来解决这个问题:问题<一href="http://stackoverflow.com/questions/18584554/listview-scrolls-quite-junky-when-therre-are-different-row-heights?rq=1">listview滚动相当毫无价值时therre不同的行高

There's a guy suggesting to write a View recycler to fix this: question listview scrolls quite junky when therre are different row heights

可否写查看回收一样非常复杂,因为它的声音?

Would writing a View recycler be as terribly complicated as it sounds?

对不起,抽象的问题。

推荐答案

只记得的ImageView和TextView的高度,然后当被回收的意见,设置高度从getView方法在ListAdapter这些意见(您需要扩展BaseAdapter这个)。

Just remember the height of the ImageView and the TextView, and then when your views are being recycled, set the height for those views from the getView method in your ListAdapter (you need to extend BaseAdapter for this).

所以,你有你的数据对象的列表(一些文字,也许图片网址等)。添加一个名为imageHeight整型字段。当加载图像列表项的第一次,ImageView的高度保存到整数字段与该列表项关联的数据对象。然后,在getView在ListAdapter,设置的ImageView的HIGHT到imageHeight的值

So you have a List of objects with your data (some text, maybe an image URL, etc). Add an integer field called imageHeight. When you load the image for a list item for the first time, save the height of the ImageView to that integer field in the data object associated with that list item. Then, in getView in your ListAdapter, set the hight of the ImageView to the value of imageHeight.

这会让你的适配器返回一个视图,它具有适当的高度。当然,这只是工作,如果你已经知道了图像/文本的HIGHT你即将加载。这将是真实的,如果你滚动起来,因为这些项目已经被加载。

This will make your adapter return a view which has the proper height. Of course, this only works if you already know the hight of the image/text you're about to load. This will be true if you're scrolling up, since those items have already been loaded.

这篇关于ListView和可变高度的项目导致的滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆