HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Grid with images in RecyclerView

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withimagesrecyclerviewgrid

Problem

I've created a Grid with a RecyclerView similar to the GoogleIO 2014's generic media player one.

GoogleIO generic music player

Mine

The thing is that the images doesn't load immediately, but with a delay, so I had to put that "dirty" code for add a crossfade every time an image is loaded.

Beside that, I just want the code to be the most efficient and clean possible.

RecyclerViewAdapter

```
public class AlbumsRecyclerViewAdapter extends CursorRecyclerAdapter {

private Context mContext;
private int mArtistColumnIndex = -1;
private int mAlbumColumnIndex = -1;
private int mIdColumnIndex = -1;
private int mDefaultTextColor, mDefaultBackgroundColor;
private int mTileElevation;
private int mImageSize = DEFAULT_IMAGE_SIZE;
private static float IMAGE_SIZE_MULTIPLIER = 0.50f;
private HashMap mPaletteCache = new HashMap<>();

private static int DEFAULT_IMAGE_SIZE = 150;

private static final int[] ATTRS = new int[]{
android.R.attr.textColorPrimaryInverse
};

public AlbumsRecyclerViewAdapter(Context context, Cursor c) {
super(c);
mContext = context;
final TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
mDefaultTextColor = typedArray.getColor(0, Color.WHITE);
typedArray.recycle();
mTileElevation = context.getResources().getDimensionPixelSize(R.dimen.tile_elevation);
mDefaultBackgroundColor = ContextCompat.getColor(context, R.color.grid_item_background);

}

@Override
public void onBindViewHolder(AlbumHolder holder, Cursor cursor) {
holder.bind(cursor);
}

@Override
public AlbumHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new AlbumHolder(LayoutInflater.from(mContext).inflate(R.layout.album_grid_item, parent, false));
}

public class AlbumHolder extends RecyclerView.ViewHolder {

private TextView mName, mArtist;
private ImageView mImage;

publi

Solution

private void getColumnsIndices(Cursor cursor) {
    if (mAlbumColumnIndex == -1 && cursor != null) {
        mAlbumColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
    }
    if (mArtistColumnIndex == -1 && cursor != null) {
        mArtistColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
    }
    if (mIdColumnIndex == -1 && cursor != null) {
        mIdColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
    }
}


If the cursor is null, you don't need to do anything in this function. So turn it into a guard clause:

private void getColumnsIndices(Cursor cursor) {
    if (cursor == null) { return; }

    if (mAlbumColumnIndex == -1) {
        mAlbumColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
    }
    if (mArtistColumnIndex == -1) {
        mArtistColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
    }
    if (mIdColumnIndex == -1) {
        mIdColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
    }
}


private void setColorsFromPalette(Palette palette, boolean animate) {
    Palette.Swatch swatch = palette.getVibrantSwatch();
    if (swatch != null) {
        if (animate) {
            ViewUtils.setBackgroundColorWithAnimation(itemView, mDefaultBackgroundColor, palette.getVibrantColor(swatch.getRgb()));
            ViewUtils.setTextColorWithAnimation(mName, mDefaultTextColor, palette.getVibrantSwatch().getBodyTextColor());
            ViewUtils.setTextColorWithAnimation(mArtist, mDefaultTextColor, palette.getVibrantSwatch().getBodyTextColor());
        } else {
            itemView.setBackgroundColor(palette.getVibrantColor(swatch.getRgb()));
            mName.setTextColor(palette.getVibrantSwatch().getBodyTextColor());
            mArtist.setTextColor(palette.getVibrantSwatch().getBodyTextColor());
        }
    }
}


Here you make 4 calls (but only 2 at most per method invocation) to palette.getVibrantSwatch().getBodyTextColor(), which is a bit of a waste to me. Try storing the result of the method call in a temporary variable. Alternatively, consider extracting changing of color values to separate functions; one for animated color changes, one for static color changes. Arguments would be the body text color and the RGB from the swatch.

Code Snippets

private void getColumnsIndices(Cursor cursor) {
    if (mAlbumColumnIndex == -1 && cursor != null) {
        mAlbumColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
    }
    if (mArtistColumnIndex == -1 && cursor != null) {
        mArtistColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
    }
    if (mIdColumnIndex == -1 && cursor != null) {
        mIdColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
    }
}
private void getColumnsIndices(Cursor cursor) {
    if (cursor == null) { return; }

    if (mAlbumColumnIndex == -1) {
        mAlbumColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
    }
    if (mArtistColumnIndex == -1) {
        mArtistColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
    }
    if (mIdColumnIndex == -1) {
        mIdColumnIndex = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
    }
}
private void setColorsFromPalette(Palette palette, boolean animate) {
    Palette.Swatch swatch = palette.getVibrantSwatch();
    if (swatch != null) {
        if (animate) {
            ViewUtils.setBackgroundColorWithAnimation(itemView, mDefaultBackgroundColor, palette.getVibrantColor(swatch.getRgb()));
            ViewUtils.setTextColorWithAnimation(mName, mDefaultTextColor, palette.getVibrantSwatch().getBodyTextColor());
            ViewUtils.setTextColorWithAnimation(mArtist, mDefaultTextColor, palette.getVibrantSwatch().getBodyTextColor());
        } else {
            itemView.setBackgroundColor(palette.getVibrantColor(swatch.getRgb()));
            mName.setTextColor(palette.getVibrantSwatch().getBodyTextColor());
            mArtist.setTextColor(palette.getVibrantSwatch().getBodyTextColor());
        }
    }
}

Context

StackExchange Code Review Q#105049, answer score: 2

Revisions (0)

No revisions yet.