Created
November 3, 2015 17:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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