android - 如何在 PagerAdapter 的 instantiateItem 方法中使用 AsyncTask

标签 android android-asynctask android-viewpager

我想使用 ViewPager 来显示一些加密的图像。为此,我创建了 PagerAdapter 的扩展,并将我的解密代码放在 instantiateItem 方法中。但问题是解密过程耗时过长。因此,我在 instantiateItem 方法中使用了 AsyncTask 来显示进度对话框。

现在 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);

    }
}

最佳答案

感谢@rtsai2000,我终于使用AsyncTaskget()方法返回了真实的结果。解决方案是等待结果 (viewLayout)。所以我的代码改为:

public class FullScreenImageAdapter extends PagerAdapter {

    private Activity _activity;
    private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;
    ImageView imgDisplay;
    View viewLayout;
    private ViewGroup container;

    // 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) {
        this.container = container;

        DisplayFile df = new DisplayFile();
        df.execute(_imagePaths.get(position));

        View result = null;

        try {
            result = df.get();
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
        }

        return result;
    }

    private class DisplayFile extends AsyncTask<String, View, View> {

        ProgressDialog progress;
        String path;
        Bitmap bitmap;

        @Override
        protected View 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 viewLayout;
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(_activity, _activity.getString(R.string.alert_decrypt_progress), 
                    _activity.getString(R.string.alert_decrypt_progress_msg), false);

            inflater = (LayoutInflater) _activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                    false);
        }

        @Override
        protected void onPostExecute(View result) {
            progress.dismiss();

            imgDisplay = (ImageView) result.findViewById(R.id.img_axew);

            imgDisplay.setImageBitmap(bitmap);
            ((ViewPager) container).addView(result);
        }

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

关于android - 如何在 PagerAdapter 的 instantiateItem 方法中使用 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161075/

相关文章:

java - 新的NotificationCompat.BigPictureStyle()显示错误无法膨胀通知 View

android - 向/从 AsyncTask 发送/接收 ArrayList

java - ViewPager 中 fragment 之间的转换工作缓慢

android - 从android中的JPEG header 中提取曝光时间

android - Kotlinx 综合和 AndroidX

php - 安卓 JSON 不工作

android - 如何处理 facebook asyncrunner 更新 UI

android - doInBackground 调用 AsyncTask 然后休眠 block UI 线程

android - 如何去除android viewpager开头和结尾的多余填充?

android - ViewPager 快速滑动给出了 IndexOutOfBoundsException