我有一個ListFragment異步加載多個圖像,但它的行爲奇怪的方式,as you can see here。Android AsyncTask奇怪的行爲
這裏的的AsyncTask代碼
public class BitmapWorkerClass extends AsyncTask<Integer, Void, Bitmap>
{
private Context context;
private final WeakReference<ImageView> imageViewWeakReference;
private int data = 0;
public BitmapWorkerClass(ImageView imageView, Context context)
{
this.context = context.getApplicationContext();
imageViewWeakReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return ImageResizer.decodeSampledBitmapFromResource(context.getResources(), data, 100,100);
}
@Override
public void onPostExecute(Bitmap bitmap)
{
if(imageViewWeakReference != null && bitmap != null)
{
final ImageView imageView = imageViewWeakReference.get();
if(imageView != null)
{
imageView.setImageBitmap(bitmap);
}
}
}
}
,我把它從ListFragment適配器的getView()方法
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_fragment_single_recipe_title, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.recipeTitleImage);
holder.title = (TextView) convertView.findViewById(R.id.recipeTitleText);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Recipe recipe = getItem(position);
loadBitmap(recipe.getImage(), holder.image);
holder.title.setText(recipe.getTitle());
return convertView;
public void loadBitmap(int resId, ImageView imageView)
{
BitmapWorkerClass task = new BitmapWorkerClass(imageView, getContext());
task.execute(resId);
}
你能幫助我理解是什麼使的AsyncTask表現爲視頻?
在此先感謝
的事實是,有沒有保證getView多少次叫 – Blackbelt
但我需要更新UI進入getView(),你能解釋一下你的意思? – basteez
發佈你的食譜和整個適配器類代碼 –