android - Get width of CardView in Adapter.onBindViewHolder -


I have a list of posts and most of them are pictures (just post it like a G + or FB app) . Each post entry has an image aspect ratio, so I can set the height of the image according to the width of it, before the image is loaded even before the server, so the card layout will not change on load.

Set the problem layout_width = "match_parent" for both card and post image. When I get the width of the cardview then it is zero. So I can not calculate the height

Now I see that the only solution takes the width of the original container and reduces all the feet, but it does not look like a good solution.

Is there any other way to do this?

The adapter code is an example of

  @Override public void onBindViewHolder (ViewHolder holder, int position) {.... int width = holder.itemView.getWidth ( );  

layout (irrelevant part)

   & Lt; / RelativeLayout & gt; & Lt; /android.support.v7.widget.CardView>  

Included in the post:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" Android: layout_width = "Match_parent" Android: layout_height = "wrap_content" & gt; & Lt; imageView android: id = "@ + id / postImage" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: layout_below = "@ + id / commenterImage" Android: minHeight = "120dp" Android: scaleType = "Sentrcop "/> & Lt; / RelativeLayout & gt;  

when onBind or onViewAttachedToWindow It is said that the child is measured not so you can not get the width. Even if these calls are measured after the child, then what you are trying to do will not be a good practice because a new measurement will be required to change the height.

If you are using LinearLayoutManager, then the width of the full child (Risaicleyr see hope padding and child margins)

Another (more flexible) approach at a Creating a custom imageview that maintains your aspect ratio When called onBind, you will set the desired ratio of your custom imageview. When called on the measurement, it depends on your aspect ratio.

  class expandes MyImageView ImageView .... private flat mScale = 1f; Public Zero SetScale (int scale) {mScale = scale; } @ Override Measures on Protected Zero (Int width mowersPEC, int heightmeasureSpec) {super. Match-maker (width mousspeak, highimacerpec); Int width = getMeasuredWidth (); Setmasharmement (width, width * MSKel); }}  

In your onBind method, you call setScale on ImageView based on your w / h ratio.

I have not tested but should act as this attitude desired.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -