Palette library changes colors when scrolling in GridView

I'm having trouble implementing the new palette library (in section 4.4.4 c 'com.android.support:palette-v7:21.0.+'

). I'm trying to color part of every item in the GridView, which works great, but when I scroll the item off the screen, it changes back to the wrong color for a few seconds before it reverts to the correct color.

I thought the problem might have been causing view.setBackgroundColor

on every call to getView, so I made a check in front of my code if it already had a rendered color. This made the situation even worse. Every time I scrolled around the colors it would change. With enough scrolling, all my colors were swapped. It seems like the colors are switching with each other and not randomly.

Here is a snippet of my code:

Palette.generateAsync(bitmap,
    new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {
            Palette.Swatch vibrant =
                    palette.getMutedSwatch();
            if (vibrant != null) {
                fView.findViewById(R.id.colored_bar).setBackgroundColor(
                        vibrant.getRgb());
            }
        }
    });

      

Does anyone know a way to get around this issue? I heard mention of caching a response from Palette, but wasn't sure if that would mean I would do more than I did. I have also tried to use the palette in sync and async. Thank.

+3


source to share


1 answer


I found a solution to this. Basically the problem was that the palette object was created every time, which was both expensive and imprecise. Since the palette is static, I created a ViewHolder and kept the palette as it was when it was created.



0


source







All Articles