Fresco: How to get actual view size before onBindViewHolder() in Recyclerview

Created on 9 Nov 2015  路  6Comments  路  Source: facebook/fresco

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!!!

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.

All 6 comments

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();

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eldk picture eldk  路  3Comments

sungerk picture sungerk  路  3Comments

kingty picture kingty  路  4Comments

goodev picture goodev  路  4Comments

eresid picture eresid  路  4Comments