question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
IanChildscommented, Nov 10, 2015

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.

7reactions
majidsa70commented, Dec 9, 2019

itemView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int width = itemView.getMeasuredWidth(); int height = itemView.getMeasuredHeight();

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the height of recyclerview item in ... - Stack Overflow
With the addOnGlobalLayoutListener() method, the height value is obtained before the TextView is drawn. And then save it in a member variable.
Read more >
Everything You Should Know To Create A Recyclerview
1. Item Click Listener · android:id="@+id/layout". After that, we will add it to Viewholder as below. · RelativeLayout layout;//Inside Constructor
Read more >
Android Fundamentals: Working with the RecyclerView ...
Often, I see many examples which use the adapter's onBindViewHolder() method to do the work on the view holder. As I mentioned earlier,...
Read more >
Android RecyclerView Tutorial with Kotlin - RayWenderlich.com
In this Android RecyclerView tutorial, learn how to use Kotlin to display datasets of a large or unknown size!
Read more >
RecyclerView.Adapter - Android Developers
Most of the time, this is a case where the transient state will be cleared in onBindViewHolder call when View is rebound to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found