我正在使用AsyncTask將圖像加載到我的ListView中。但是,當我運行該應用程序時,圖片的順序會隨着滾動而改變。我懷疑getView()方法可能是錯誤的。但我找不到解決這個問題的方法。在ListView中滾動時加載AsyncTask的圖像順序發生變化
對於Android來說相對較新,我無法探索很多選擇。
這裏是我的代碼:
DisplayPlacements.java
package com.example.android.areainfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
public class DisplayPlacements extends AppCompatActivity {
ListView listView;
private String[] imageURLArray = new String[]{
"http://farm8.staticflickr.com/7315/9046944633_881f24c4fa_s.jpg",
"http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
"http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
"http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
"http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
"http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
"http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
"http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
"http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
"http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
"http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
"http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg",
"http://farm8.staticflickr.com/7315/9046944633_881f24c4fa_s.jpg",
"http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
"http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
"http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
"http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
"http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
"http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
"http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
"http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
"http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
"http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
"http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_placements);
listView = (ListView) findViewById(R.id.listPlacements);
ImageAdapter imageAdapter = new ImageAdapter(this, R.layout.row, imageURLArray);
listView.setAdapter(imageAdapter);
}
}
activity_display_placements.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_placements"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.areainfo.DisplayPlacements">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listPlacements">
</ListView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/testImage"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop" />
</LinearLayout>
ImageAdapter.java
package com.example.android.areainfo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.io.IOException;
import java.net.URL;
/**
* Created by User on 30-Jan-17.
*/
public class ImageAdapter extends ArrayAdapter<String> {
private String[] imageURLArray;
private LayoutInflater inflater;
public ImageAdapter(Context context, int textViewResourceId,
String[] imageArray) {
super(context, textViewResourceId, imageArray);
// TODO Auto-generated constructor stub
inflater = ((Activity)context).getLayoutInflater();
imageURLArray = imageArray;
}
private static class ViewHolder {
String imageURL;
ImageView imageView;
Bitmap bitmap;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.testImage);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.imageURL = imageURLArray[position];
new DownloadAsyncTask().execute(viewHolder);
return convertView;
}
private class DownloadAsyncTask extends AsyncTask<ViewHolder, Void, ViewHolder> {
@Override
protected ViewHolder doInBackground(ViewHolder... params) {
// TODO Auto-generated method stub
//load image directly
ViewHolder viewHolder = params[0];
try {
URL imageURL = new URL(viewHolder.imageURL);
viewHolder.bitmap = BitmapFactory.decodeStream(imageURL.openStream());
} catch (IOException e) {
// TODO: handle exception
Log.e("error", "Downloading Image Failed");
viewHolder.bitmap = null;
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder result) {
// TODO Auto-generated method stub
if (result.bitmap == null) {
// result.imageView.setImageResource(R.drawable.postthumb_loading);
} else {
result.imageView.setImageBitmap(result.bitmap);
}
}
}
}
幫助讚賞。
謝謝!
使用'Picasso' http://square.github.io/picasso/ –
請加一線得到視圖 字符串mImageURL = this.getItem(位置); viewHolder.imageURL = mImageURL; // imageURLArray [position]; 再更換第二線,同時增加圖像的URL到viewholder –
你的ListView的項目得到回收/滾動時重用。如果你已經讀了一下這個網站,你會遇到這一百次。幾乎每一位程序員都偶然發現這一點。所以谷歌和搜索。有這麼多的準備好用於你的問題的例子。 – greenapps