Search code examples
androidgridviewpicasso

View lags while Picasso puts image


im using Picasso for loading my images into an ExpandableHeightGridView .

When Picasso wants to put bitmap into the GridView ( ImageViews ) , it causes a lot of lag . My adapter follows performance guidelines such as ViewHolder . I tried to use Resize , crop and target as well but no difference , any ideas whats the cause and how can i fix it ?

    package Adapter;


    public class StacksAdapter extends ArrayAdapter<Stack> {

    GradientDrawable gd;

    private Context mContext;
    int width;
    int height;
    Boolean Tablet;

    public StacksAdapter(Context c, int resourceId, List<Stack> items) {
        super(c, resourceId, items);
        mContext = c;

        gd = new GradientDrawable();
        gd.setColor(Color.GRAY);


    }

    static class ViewHolderItem {
        ImageView image_front ;
        FlatTextView Textview;
        RelativeLayout rl;

        public void setImage(final ArrayAdapter<Stack> adapter,
                final Stack item, final int position, ImageView imgView,
                final String id) {

            String url = "some url" ;

            Utility.customPicasso.load(url).placeholder(gd).resize(120, 150)
                    .into(imgView, new Callback() {

                        @Override
                        public void onSuccess() {
                            // doing something
                        }

                        @Override
                        public void onError() {

                        }
                    });
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolderItem ViewHolder;
        RelativeLayout retval = null;
        Stack stack = getItem(position);

        if (convertView != null) {
            retval = (RelativeLayout) convertView;
            ViewHolder = (ViewHolderItem) convertView.getTag();
        }

        else {

            ViewHolder = new ViewHolderItem();
            LayoutInflater mInflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            retval = (RelativeLayout) mInflater.inflate(R.layout.stack_object,
                    null);

            ViewHolder.image_back = (ImageView) retval
                    .findViewById(R.id.stack_object_image2);

            ViewHolder.rl = (RelativeLayout) retval
                    .findViewById(R.id.stack_object_rl);

        }

        ViewHolder.Textview.setText(stack.title);
        ViewHolder.rl.setPadding(30, 0, 30, 0);

        ViewHolder.setImage(this, stack, position,
                        ViewHolder.image_front, stack.image_front_id);

        retval.setTag(ViewHolder);

        return retval;
    }
}

and here is my custom Picasso , i configure it once in app startup , which i only set header for its connections :

    public static void ConfigurePicasso(Context context) {

    OkHttpDownloader downloader = new OkHttpDownloader(context) {

        @Override
        protected HttpURLConnection openConnection(Uri uri)
                throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            connection.setRequestProperty("something",
                    some value);
            return connection;
        }
    };

    Picasso.Builder builder = new Picasso.Builder(context);
    builder.downloader(downloader);
    builder.requestTransformer(RequestTransformer.IDENTITY);
    builder.memoryCache(new LruCache(context));
    builder.executor(Executors.newSingleThreadExecutor());
    customPicasso = builder.build();
}

Solution

  • I figured it out ! i had some db transactions in my adapter , i commented them and now it works brilliantly . I knew opening and closing database could cost resource but the problem was the lag was ONLY at setImageBitmap time not any other times . still a mistery for me but finally done .

    Now i save all my data from database in a list then use that list inside my getView . very smooth .