我正在使用 ListView 并将自定义对象扩展到 getView 方法中。
当 ListView 触发 onItemClicked 事件时,我的问题开始,它会重新绘制项目。
这不是问题。出现实际问题是因为我正在回收 getView 给我的项目。
在开始之前,这是我正在使用的代码。
public View getView(int position, com.abc.rxcard.Item itemAtPosition,
View convertView, ViewGroup parent) {
LinearLayout thisItem;
//Get the current alert object
//Inflate the view
if(convertView==null)
{
thisItem = new LinearLayout(this);
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)this.getSystemService(inflater);
vi.inflate(R.layout.offerrow, thisItem, true);
}
else
{
thisItem = (LinearLayout) convertView;
}
Log.d("thisItem","This Item is Index " + position + " " + thisItem.toString() + " " + Integer.toHexString(System.identityHashCode(thisItem)));
//Get the text boxes from the listitem.xml file
TextView caption =(TextView)thisItem.findViewById(R.id.caption);
TextView subText =(TextView)thisItem.findViewById(R.id.subText);
ImageView icon = (ImageView)thisItem.findViewById(R.id.offerIcon);
//Assign the appropriate data from our alert object above
caption.setText(itemAtPosition.text());
subText.setText(itemAtPosition.subText());
MyUtils.setImageFromURLInBackgroundThread(icon, itemAtPosition.icon());
return thisItem;
}
当我点击这些项目时,问题就出现了。 日志输出如下
04-20 14:36:28.402: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:28.432: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:28.442: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44d2ee48 44d2ee48
04-20 14:36:28.932: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44d2ee48 44d2ee48
04-20 14:36:28.952: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:28.962: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:29.622: DEBUG/thisItem(12495): This Item is Index 0 android.widget.LinearLayout@44db1c50 44db1c50
04-20 14:36:29.652: DEBUG/thisItem(12495): This Item is Index 1 android.widget.LinearLayout@44da9038 44da9038
04-20 14:36:29.662: DEBUG/thisItem(12495): This Item is Index 2 android.widget.LinearLayout@44d2ee48 44d2ee48
如您所见,索引 0 和 2 正在来回翻转项目。现在,如果这些项目实际上不在屏幕上,这将不再是问题。但是,当我单击这些项目时(并且因为图像是在后台线程中加载的),每次单击时,项目 1 和 3 上的图像都会来回翻转。
这似乎是一种不好的行为。
我不知道该怎么做才能解决这个问题。我可以在 ListView 上放置一个设置或其他东西来阻止它这样做吗?
最佳答案
事实证明,回收商正在检查我的所有元素并将它们放入废料堆中,因为它将在本次运行周期中刷新它们。我遇到的问题是因为它以相反的顺序遍历项目,我应该理所当然地认为我需要删除该对象,然后再次设置其值。
我对这个问题的解决方案是将我下载到磁盘的图像缓存在指定位置至少一分钟。当刷新发生时,它将尝试从缓存加载图像,而不是每次都下载它。并且仅当图像过期时才会下载。从而立即用所需的图像替换图像,而不是保留现有的 fragment 图像。
我面临的问题已经解决,但事实仍然是他们回收了屏幕上的项目....很难相信在一个项目上单击整个 ListView 将被刷新并且所有项目都会刷新被重新创建。但这就是 cookie 崩溃的方式,当你将它浸入 java 中时......
关于java - Android ListView 正在回收仍在屏幕上的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5737027/