我花了半天时间阅读文档和示例。我找不到解决方案。我在 Activity 中有一个 ListView,每个项目都有 LinearLayout,背景图像由 nostr13 用 Android-Universal-Image-Loader 加载。
我使用这个库将 LinearBackground 图像作为背景。在我的活动中,我下载了一个 JSON,使用 URI 解析和创建一个字符串数组。
问题是图书馆的打印是错误的!它适用于 ImageView 但如果我将 LinearBackground 与 loadImage() 一起使用而不是 displayImage() 它不会正确放置背景。慢动作看到的过程是这样的:创建正确数量的元素(4),将第一个加载的图像作为一行的背景,然后一旦加载另一个图像,库就会用这个新图像替换旧背景,不同,然后它放置另一个图像并停止。这是一个错误的例子。总是不一样!!!其中一些是空的,但会调用 onLoadingComplete 事件。我认为这是缓存的问题,我玩过 UIL 的选项,但没有运气。图片只有 4 张,非常小(每张 <10kb)。
我在 onPostExecute 事件中设置了适配器,可以吗?
可能是什么问题?
这里是重要的代码:
活动代码(我已删除不相关的代码以帮助查找问题):
public class myActivity extends Activity {
Context context;
// Output Vars
String mStrings[];
ListView listView;
// UIL
DisplayImageOptions options;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_stagioni);
listView = (ListView) findViewById(R.id.listView);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.fail)
.showImageOnFail(R.drawable.fail)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.build();
startAsynkTask(context);
}
public void startAsynkTask(final Context context) {
AsyncTask<String, Void, Void> task = new AsyncTask<String, Void, Void>() {
private ProgressDialog pd;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(ActivityStagioni.this);
pd.setTitle(getResources().getString(R.string.dialog_loading));
pd.setMessage(getResources().getString(R.string.dialog_loading_stagioni));
pd.setCancelable(true);
pd.setCanceledOnTouchOutside(true);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected Void doInBackground(String... arg0) {
//... Not relevant code ...
}
@Override
protected void onPostExecute(Void result) {
pd.dismiss();
((ListView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener()
{
//... Not relevant code ...
});
}
};
task.execute();
}
public class ImageAdapter extends BaseAdapter {
ImageLoader imageLoader;
public ImageAdapter() {
imageLoader = ImageLoader.getInstance();
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = getLayoutInflater().inflate(R.layout.stagioni_item, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (LinearLayout) view.findViewById(R.id.linearLayoutLocandina);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
imageLoader.loadImage(mStrings[position], new SimpleImageLoadingListener() {
//final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
//if (loadedImage != null) {
// boolean firstDisplay = !displayedImages.contains(imageUri);
// if (firstDisplay) {
BitmapDrawable background = new BitmapDrawable(loadedImage);
holder.imageView.setBackgroundDrawable(background);
//}
// }
}
});
return view;
}
ListView 项目是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="horizontal"
android:weightSum="5"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/linearLayoutLocandina"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_weight="5" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_weight="5"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:alpha="1"
android:background="#80000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_NumeroStagione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|center_horizontal"
android:text="30"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_NomeStagione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_weight="0"
android:text="TextView"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView_NumeroEpisodi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_objectID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:layout_gravity="bottom"
style="@style/ProgressBarStyle" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
应用
public class MyAppName extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader(getApplicationContext());
}
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}
}
Logcat http://pastebin.com/JejB8miC
谢谢你的支持