我想使用ViewPager
显示一些加密的图像。为此,我创建了PagerAdapter
的扩展名并将解密代码放入instantiateItem
方法中。但是问题是解密过程花费的时间太长。因此,我在AsyncTask
方法内使用了instantiateItem
来显示进度对话框。
现在,ViewPager
首先显示一个空白屏幕,我应该滑动以查看第一张图像。而且setCurrentItem()
不起作用。
我的代码:
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
ImageView imgDisplay;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.img_axew);
new DisplayFile().execute(_imagePaths.get(position));
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
private class DisplayFile extends AsyncTask<String, Integer, Integer> {
ProgressDialog progress;
String path;
Bitmap bitmap;
@Override
protected Integer doInBackground(String... arg0) {
path = arg0[0];
File file = new File(path);
try {
bitmap = AxerProcessor.getBitmapFromByte(AxerProcessor.decryptBytes(AxewActivity.password, AxerProcessor.decompressBytes(AxerProcessor.getFileBytes(file))));
} catch (Exception e) {
Toast.makeText(_activity, _activity.getString(R.string.error_wrong_password).replace("%s", file.getName()), Toast.LENGTH_LONG).show();
}
return null;
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(_activity, _activity.getString(R.string.alert_decrypt_progress),
_activity.getString(R.string.alert_decrypt_progress_msg), false);
}
@Override
protected void onPostExecute(Integer result) {
progress.dismiss();
imgDisplay.setImageBitmap(bitmap);
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
最佳答案
我认为您缺少对PagerAdapter.notifyDataSetChanged()的调用-http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#notifyDataSetChanged()
您可以调用notifyDataSetChanged():
在您致电.addView()之后[在这种情况下,您首先出现空白屏幕]或
您可以将更多代码(膨胀视图,addView)移到onPostExecute中,以便在完全准备好渲染图像之前什么也不会发生。