Adapter的instantiateItem方法内使用Asyn

Adapter的instantiateItem方法内使用Asyn

本文介绍了如何在PagerAdapter的instantiateItem方法内使用AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to use ViewPager to display some encrypted images. For that I created an extension of PagerAdapter and put my decryption code inside instantiateItem method. But the problem is that the process of decryption takes too long. So, I used AsyncTask inside instantiateItem method to show a progress dialog.

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

I think you are missing a call to PagerAdapter.notifyDataSetChanged() - http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#notifyDataSetChanged()

您可以调用notifyDataSetChanged():

You can call notifyDataSetChanged():

  • 在调用.addView()后立即出现[在这种情况下,您首先出现空白屏幕],或者
  • 您可以将更多代码(膨胀视图,addView)移到onPostExecute中,以便在完全准备好渲染图像之前什么也不会发生.

这篇关于如何在PagerAdapter的instantiateItem方法内使用AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 00:01