即使在通用图像加载器中加载了图像之后,有时也会显示progressdialog

时间:2013-12-30 11:48:32

标签: android android-viewpager universal-image-loader

下面的代码显示了进度对话框,即使图像已加载。如何解决?我非常感谢任何帮助。谢谢你。

MainActivity:

public class MainActivity extends Activity {

      final String[] page_titles = new String[]{"Title1", "Title2", "Title3", "Title4", "Title5"};
        //this will go the description TextView
            final String[] desc = new String[]{
                "desc1",
                "desc2",
                "desc3",
                "desc4",
                "desc5"
        };


            ImageView im;

            String[] urls = {
                    "1.jpg",
                    "2.jpg",
                    "3.jpg",
                    "4.jpg",
            };

    ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         //Instantiating the adapter
        myAdaptertest1 mAdapter = new myAdaptertest1(this);

        vp=(ViewPager)findViewById(R.id.pager);

        vp.setAdapter(mAdapter);

        ////////




    }

适配器:

        private class myAdaptertest1 extends PagerAdapter{

        Context context;
        ImageLoader imageLoader;
        public myAdaptertest1(Context c){
            this.context = c;

       DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .build();
           imageLoader=  ImageLoader.getInstance();
            imageLoader.init(config);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
              return page_titles.length;
        }

        @Override
        public boolean isViewFromObject(View v, Object o) {
            // TODO Auto-generated method stub
              return v.equals(o);
        }





        public Object instantiateItem(View pager, int position) {
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View vi = inflater.inflate(R.layout.page, null, false);

final ProgressBar spinner = (ProgressBar) vi.findViewById(R.id.loading);
            im = (ImageView)vi.findViewById(R.id.imageView1);
imageLoader.displayImage(urls[position],im, defaultOptions,new SimpleImageLoadingListener(){
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                            break;
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }
                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                }
            });

            TextView title = (TextView)vi.findViewById(R.id.tvTitle);
            TextView description = (TextView) vi.findViewById(R.id.tvdesc);

             im = (ImageView)vi.findViewById(R.id.imageView1);


            title.setText(page_titles[position]);
            description.setText(desc[position]);

                       String rt = urls[position];
            imageLoader.displayImage(rt, im);//<---here just call this function
            //This is very important
            ( (ViewPager) pager ).addView( vi, 0 );

            return vi;
        }

        @Override
        public void destroyItem(View pager, int position, Object view) {
            ((ViewPager) pager).removeView((View) view);
        }

0 个答案:

没有答案