ViewPager显示最后的图像,直到新图像未加载

时间:2015-09-23 07:08:55

标签: android android-fragments android-viewpager android-view

我的问题:第一次打开项目屏幕时,所有图像将被加载到viewpager中,之后当我打开新项目屏幕时,它会显示旧图像,直到新图像未加载。

所以当我打开一个新项目时,我想清除viewpager上的旧图像。

当我打开新项目屏幕时,如何清除最后的图像?

我在viewpager中为显示图像编写了以下代码:

   class productimages extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... arg) {

            imageUlrList.clear();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id", arg[0]));

            JSONObject json = jParser.makeHttpRequest(url, "GET",
                    params);

            try {
                int success;

                success = json.getInt(TAG_IMAGESUCCESS);
                if (success == 1) {

                    JSONArray productObj = json.getJSONArray(TAG_PIMAGES);

                    for (int index = 0; index < productObj.length(); index++) {
                        JSONObject product = productObj.getJSONObject(index);

                        imageurl = product.getString(TAG_IMAGEURL);
                        imageUlrList.add(imageurl);
                    }

                } else {
                    // product with pid not found
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {

            try {
                mViewPager.setAdapter(null);
                mViewSlidingImageAdapter = new ViewSlidingImageAdapter(
                        getSupportFragmentManager());
                mViewPager.setAdapter(mViewSlidingImageAdapter);
                mViewSlidingImageAdapter.notifyDataSetChanged();
            } catch (Exception e) {
            }

        }
    }

我的适配器类:

  class ViewSlidingImageAdapter extends FragmentStatePagerAdapter {

        public ViewSlidingImageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return imageUlrList.size();
        }

        @Override
        public Fragment getItem(int position) {

            return ViewSlidingImage.newInstance(position, product_url);
        }


    }

ViewSlidingImage:

public class ViewSlidingImage extends Fragment {

    private String postionOfArray = "";

    static ViewSlidingImage newInstance(int imageNum, String imagePathName) {
        final ViewSlidingImage f = new ViewSlidingImage();
        final Bundle args = new Bundle();

        args.putString("postionOfArray", "" + imageNum);
        f.setArguments(args);
        return f;
    }

    public ViewSlidingImage() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        postionOfArray = getArguments().getString("postionOfArray");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.view_sliding_image, container,
                false);
        ImageView mImageView = (ImageView) v
                .findViewById(R.id.tutorial_imageview);


        try {

            String imagePath = SingleProductscreen.imageUlrList.get(Integer
                    .parseInt(postionOfArray));
            UrlImageViewHelper.setUrlDrawable(mImageView, imagePath);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return v;
    }
}

1 个答案:

答案 0 :(得分:1)

ViewSlidingImageImageView的内容加载到imageUrlList,如下所示:

String imagePath = SingleProductscreen.imageUlrList.get(Integer.parseInt(postionOfArray));

所以您需要做的就是清除SingleProductscreen.imageUlrList中的onPreExecute。通过这种方式,您可以确保一旦调用productimages asynctask,SingleProductscreen.imageUlrList为空,因此不会加载任何图像,它将为空白。

    protected void onPreExecute() {
        super.onPreExecute();
        SingleProductscreen.imageUlrList.clear();
        mViewSlidingImageAdapter = new ViewSlidingImageAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewSlidingImageAdapter);
        mViewSlidingImageAdapter.notifyDataSetChanged();
    }