DF1E / SimpleExplorer

Open Source Filemanager
http://forum.xda-developers.com/showthread.php?t=2330864
GNU General Public License v3.0
138 stars 62 forks source link

[Solution] Lag when scrolling above directory with a lot of files #85

Open traabefi opened 8 years ago

traabefi commented 8 years ago

There is a simple solution for this problem. First off, we need to implement Picasso pibrary, because, why do we invent wheel when it's already invented ? So this is how the getFileIcon method looks now:

public static void getFileIcon(File file, final ImageView icon, int numberOfFiles) {
            if (Settings.showThumbnail() & isvalidMimeType(file)) {
            Picasso.with(mContext).load(file).into(icon);
        } else {
            loadFromRes(file, icon, numberOfFiles);
        }
    }

here is improved version of loadFromRes() method, where we already know number of files so we don't count them again (We save a lot of memory):


private static void loadFromRes(final File file, final ImageView icon, final int numberOfFiles) {
        Drawable mimeIcon = null;
        if (file != null) {
            if (file.isDirectory()) {
                if (file.canRead() && numberOfFiles > 0) {
                    mimeIcon = mResources.getDrawable(R.drawable.type_folder);
                } else {
                    mimeIcon = mResources.getDrawable(R.drawable.type_folder_empty);
                }
            } else if (file.isFile()) {
                final String fileExt = SimpleUtils.getExtension(file.getName());
                mimeIcon = mMimeTypeIconCache.get(fileExt);

                if (mimeIcon == null) {
                    final int mimeIconId = MimeTypes.getIconForExt(fileExt);
                    if (mimeIconId != 0) {
                        mimeIcon = mResources.getDrawable(mimeIconId);
                        mMimeTypeIconCache.put(fileExt, mimeIcon);
                    }
                }
            }
        }
        if (mimeIcon != null) {
            icon.setImageDrawable(mimeIcon);
        } else {
            // default icon
            icon.setImageResource(R.drawable.type_unknown);
        }
    }  

and finally, my solution to loading file information with icon, yeah as you already guessed, we need AsyncTask with a callback:

static class CountFiles extends AsyncTask<File, Void, Integer> {
        public AsyncResponse delegate = null;

        public interface AsyncResponse {
            void processFinish(int output);
        }

        public CountFiles(AsyncResponse delegate) {
            this.delegate = delegate;
        }

        @Override
        protected Integer doInBackground(File... params) {
            File data = params[0];
            if (data != null && data.isDirectory()) {
                return data.list().length;
            } else {
                return 0;
            }
        }

        @Override
        protected void onPostExecute(Integer count) {
            delegate.processFinish(count);
        }
    }

and last piece of code:

AsyncTask<File, Void, Integer> numberOfFiles = new CountFiles(new CountFiles.AsyncResponse() {
            @Override
            public void processFinish(int output) {
                IconPreview.getFileIcon(file, mViewHolder.icon, output);

                if (file.isFile()) {
                    // Shows the size of File
                    mViewHolder.bottomView.setText(SimpleUtils.formatCalculatedSize(file.length()));
                } else {
                    // show the number of files in Folder
                    mViewHolder.bottomView.setText(output
                            + mResources.getString(R.string.files));
                }
            }
        }).execute(file);

that should be all, enjoy