代码片段分享

RecycleView 快速滑动(包括拖拽fast scroll

2020-10-13  本文已影响0人  花艺荣

用RecycleView 做的本地图片展示功能,图片数量较多,且小图模式加载一屏有100多张图片, 用的UIL加载框架,当用手指大幅度拖拽RecycleView 或者快速拉动fast scroll bar时,内容加载缓慢。

解决:

mImagesListView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                switch (newState){
                    case SCROLL_STATE_IDLE: // The RecyclerView is not currently scrolling.
                        //当屏幕停止滚动,加载图片
                        try {
                            ImageLoader.getInstance().resume();
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case SCROLL_STATE_DRAGGING: // The RecyclerView is currently being dragged by outside input such as user touch input.
                    case SCROLL_STATE_SETTLING: // The RecyclerView is currently animating to a final position while not under outside control.
                        //由于用户的操作,屏幕产生惯性滑动,停止加载图片
                        //当屏幕滚动且用户使用的触碰或手指还在屏幕上,停止加载图片
                        try {

                            ImageLoader.getInstance().pause();
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        mImagesListView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener(){
            @Override
            public void onChildViewAttachedToWindow(View view) {

                if(view.findViewById(R.id.item_iv)!=null && (view.findViewById(R.id.item_iv)).getTag()!=null){
                    final String url = (view.findViewById(R.id.item_iv)).getTag().toString();
                    DisplayImageOptions options = options_local;

                    ImageLoader.getInstance().displayImage(url, (ImageView) view.findViewById(R.id.item_iv), options,new SimpleImageLoadingListener() {

                        @Override
                        public void onLoadingStarted(String imageUri, View view) {

                            // TODO Auto-generated method stub
                            super.onLoadingStarted(imageUri, view);
                        }
                        @Override
                        public void onLoadingFailed(
                                String imageUri, View view,
                                FailReason failReason) {
                            // TODO Auto-generated method stub

                            int iconID  = LocalResource.getInstance().GetDrawableID("icon_large_image");

                            if(url.toLowerCase().endsWith("video")){
                                iconID = LocalResource.getInstance().GetDrawableID("icon_large_video");
                            }
                            String iconuri = "drawable://"+iconID;
                            ImageLoader.getInstance().displayImage(iconuri+"", (ImageView) view.findViewById(R.id.item_iv), options_local);

                        }
                        @Override
                        public void onLoadingComplete(
                                String imageUri, View view,
                                Bitmap loadedImage) {
                            // TODO Auto-generated method stub

                            super.onLoadingComplete(imageUri, view, loadedImage);
                        }

                        @Override
                        public void onLoadingCancelled(
                                String imageUri, View view) {
                            // TODO Auto-generated method stub
                            super.onLoadingCancelled(imageUri, view);
                        }
                    });
                }
            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {

                ImageLoader.getInstance().cancelDisplayTask((ImageView) view.findViewById(R.id.item_iv));
            }
        });
上一篇下一篇

猜你喜欢

热点阅读