我知道以前有人问过这个问题,我已经对此进行了很多搜索,但无法就我的问题得到任何正确的答案。
我创建了一个列表,列表中填充了来自JSON
. 现在在getView()
方法内部,我正在用数据膨胀我的自定义行。每行包含一个Thumbnail
,我正在从不同的线程创建每个缩略图。
现在的问题是,在我不滚动列表之前,一切都很顺利。当我滚动我的列表时,我的getView()
方法被连续调用,并且所有缩略图都被重新创建并且它的位置被打乱了。创建缩略图后,我不想重新创建它们,而且我想保持缩略图的顺序。
你们能帮我解决这个问题吗?
任何帮助将不胜感激。
我的getView()
方法是:
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item_row, parent,
false);
viewHolder.titleText = (TextView) convertView
.findViewById(R.id.titleText);
viewHolder.thumbImage = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String thumbnailURLString = mPostsList.get(position).get(
"thumb");
createBitmapThread = (Thread) getLastNonConfigurationInstance();
createBitmapThread = new MyThread(thumbnailURLString,
viewHolder.thumbImage);
createBitmapThread.start();
viewHolder.titleText.setText(title);
return convertView;
}
和Thread
班级:
public class MyThread extends Thread {
String mThumbURLString = null;
ImageView mImageView = null;
public MyThread(String thumbnailURLString, ImageView imageView) {
mThumbURLString = thumbnailURLString;
mImageView = imageView;
}
@Override
public void run() {
try {
URL newurl = null;
try {
newurl = new URL(mThumbURLString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// Options opts = new Options();
/*
* opts.inDither = true; opts.inInputShareable = true;
* opts.inJustDecodeBounds = true; opts.inPurgeable =
* true;
*/
Log.e("", "INSIDE IMAGE DOINBG");
mBitmap = BitmapFactory
.decodeStream((InputStream) newurl.getContent());
} catch (IOException e) {
e.printStackTrace();
}
mHandler.post(new MyRunnable(mImageView));
} finally {
}
}
public class MyRunnable implements Runnable {
ImageView mImageView = null;
public MyRunnable(ImageView imageView) {
mImageView = imageView;
}
public void run() {
mImageView.setImageBitmap(mBitmap);
}
}