Giter Site home page Giter Site logo

asyncloading's Introduction

AsyncLoading


This project can help you load Image in ListView. Thanks to my teacher Rio who helped me restructured this project.

It will not download or load Image until slide is stop. And it will only load Image on visible screen area.

And you can use like this.

This is not easy because you should write adapter by yourself, and of course, you can extends out AsyncLoadingListAdapter and do someting. So it chould be easy when you use it. And we are making it used easily.

listView.setAdapter(new AsyncLoadingListAdapter(context) {
            private List<List<ImageResource>> imageResources;

            @Override
            public Collection<ImageResource> getImageResources(int position) {
                if (null == imageResources) {
                    imageResources = new ArrayList<>();

                    int count = getCount();
                    for (int i = 0; i < count; i++) {
                        ImageResource imageResource = new ImageResource();
                        imageResource.setPosition(i);
                        imageResource.setUrl(data[i]);

                        List<ImageResource> resources = new ArrayList<>();
                        resources.add(imageResource);

                        imageResources.add(resources);
                    }
                }
                return imageResources.get(position);
            }

            /**
             * Load over
             * @param imageResource ImageResource对象
             */
            @Override
            public void onLoadEnd(ImageResource imageResource) {
                if (ImageResource.LOAD_STATE_LOADED != imageResource.getLoadState())
                    return;

                int position = imageResource.getPosition();
                int firstVisibleItem = listView.getFirstVisiblePosition();
                int lastVisiblePosition = listView.getLastVisiblePosition();
                if (position < firstVisibleItem || position > lastVisiblePosition)
                    return;

                View view = listView.getChildAt(position - firstVisibleItem);
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                imageView.setImageBitmap(mBitmapLoader.get(imageResource.getUrl()));
                setAnimation(imageView);
            }

            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public Object getItem(int position) {
                return data[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView;
                if (null == convertView) {
                    view = layoutInflater.inflate(R.layout.list_item, null);
                }
                // do setText and setImage operation
                return view;
            }
        });

        listView.setOnScrollListener(listView.new OnScrollListenerImpl());

asyncloading's People

Contributors

lumenghz avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.