i'm loading some big image in a recyclerview.
i wanna resize it before it set to a SimpleDraweeView.
Question is i can not get the actual view size before that.
My item view layout is:
<***.SquaredFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</***.SquaredFrameLayout>
the SquaredFrameLayout is a squared layout base on it's width
My adapter is like:
@Override
public void onBindViewHolder(@NonNull List<Object> items, int position,
@NonNull ViewHolder holder) {
FeedViewHolder viewHolder = (FeedViewHolder) holder;
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(URI1)
.setResizeOptions(new ResizeOptions(*, *))
.build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(viewHolder.mSimpleDraweeView.getController())
.build();
viewHolder.mSimpleDraweeView.setController(controller);
}
I cannot get size just like view.getWidth()orview.getLayoutParams().width.
it will return 0
How can I get actual view size before onBindViewHolder()??
Thanks for your help!!!
If the view did not go through layout phase yet (which is common with onBindViewHolder), there won't be a measured dimension. To solve this, you need to add a listener for layout on ViewTreeObserver.
There is also a faster and efficient alternative, where you can pass the recyclerview width to the adapter and apply simple math to access the view width
can anybody helps?
I suggest you do as @gokhanbarisaker suggested and pass the width into the adapter.
Also, when someone tries to help you I don't think responding "NONONONO!" is really going to encourage them to help you again in the future.
I'm so sorry about that!
I did not mean that.
thanks again
No worries :)
itemView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int width = itemView.getMeasuredWidth();
int height = itemView.getMeasuredHeight();
Most helpful comment
I suggest you do as @gokhanbarisaker suggested and pass the width into the adapter.
Also, when someone tries to help you I don't think responding "NONONONO!" is really going to encourage them to help you again in the future.