Skip to content

Instantly share code, notes, and snippets.

@msomu
Created November 3, 2015 17:35
Show Gist options
  • Select an option

  • Save msomu/8867812ee73209531014 to your computer and use it in GitHub Desktop.

Select an option

Save msomu/8867812ee73209531014 to your computer and use it in GitHub Desktop.
This gist will show you how to make staggeredgridlayout with images height fixed.
@Override
public void onBindViewHolder(ViewHolderDetail holder, int position) {
DataModel item = items.get(position);
if (item.getHeight() != 0 && item.getWidth() != 0) {
float width = activity.getWindowManager().getDefaultDisplay().getWidth();
float margin = ConvertionUtil.convertDpToPixel(8, activity);
width = (width - (3 * margin)) / 2;
float i = width / ((float) item.getWidth());
float imageHeight = i * (item.getHeight());
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.image.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) width;
holder.image.setLayoutParams(params);
Picasso.with(holder.image.getContext()).load(item.getImg()).into(holder.image);
holder.itemView.setTag(item);
} else {
holder.image.setVisibility(View.GONE);
}
holder.text.setText(item.getMessage());
}
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment