Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何使用你的这个框架实现网络图片自动滚动 #1

Closed
angelen10 opened this issue Mar 2, 2015 · 5 comments
Closed

如何使用你的这个框架实现网络图片自动滚动 #1

angelen10 opened this issue Mar 2, 2015 · 5 comments

Comments

@angelen10
Copy link

我想使用你的这个框架并且使用Android Universal Image Loader来加载图片,做出类似于淘宝、京东那些活动Banner,但是会报错
java.lang.ArithmeticException: divide by zero
代码如下:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_index1, container, false);
        ButterKnife.inject(this, view);

        final List<View> banners = new ArrayList<>();
        View imageLayout = LayoutInflater.from(getActivity()).inflate(R.layout.listitem_banner, container, false);
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.imageView);

        for (int position = 0; position < imageUrls.length; position++) {
            ImageLoader.getInstance().loadImage(imageUrls[position], new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    imageView.setImageBitmap(loadedImage);
                    banners.add(view);
                }
            });
        }
        banner.setViewPagerViews(banners);

        return view;
    }
@bingoogolapple
Copy link
Owner

加载网络图片是异步执行的,当执行到banner.setViewPagerViews(banners)的时候,还没有执行banners.add(view),所以在BGABanner中除以mViews.size()会报错divide by zero


建议先初始化所有的ImageView并添加到banners集合中,然后通过ImageLoader.getInstance().displayImage(uri, imageView, options)方法去加载图片,而不是在图片加载完成后才添加ImageView到banners集合中

@bingoogolapple
Copy link
Owner

参考SplashActivity.java文件中添加lastView和给lastView中的子控件添加点击事件的实现方式 32-42行

@angelen10
Copy link
Author

@bingoogolapple 恩恩,可以实现了,你的代码很棒,借鉴了~

@bingoogolapple
Copy link
Owner

Thanks:smile:第一次有人引用我的代码

@angelen10
Copy link
Author

@bingoogolapple 码主加油fighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants