首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecyclerView + StaggeredGridLayout +毕加索获取GridItem宽度

RecyclerView + StaggeredGridLayout +毕加索获取GridItem宽度
EN

Stack Overflow用户
提问于 2015-02-03 23:52:59
回答 2查看 751关注 0票数 0

我使用RecyclerViewStaggeredGrid (spanCount是可变的)。每个GridItem (一个CardView)包含一个正方形ImageView。我想要的是加载一个图像从我的后端与所需的宽度和高度,以完全适合CardView内的ImageView。有什么建议我可以做到这一点吗?代码示例如下:

代码语言:javascript
复制
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    Picasso.with(context)
        .load(String.format("http://mybackend.com/image/w_%d,h_%d/%s",
                holder.image.getMeasuredWidth(), // This returns 0, but i want here a real width
                holder.image.getMeasuredWidth(),
                myCollection.get(position).getImagePid()))
        .tag(context)
        .placeholder(R.drawable.placeholder_logo)
        .into(holder.image);

简而言之:在绑定我的ViewHolder之前,我需要以某种方式获得imageView维度(宽度)。

EN

回答 2

Stack Overflow用户

发布于 2015-02-03 23:59:11

你有没有研究过使用.fit()?我相信这将调整图像的大小,使其完全适合ImageView。

票数 1
EN

Stack Overflow用户

发布于 2015-04-03 01:35:01

您可以使用ViewTreeObserver

代码语言:javascript
复制
final int width = holder.image.getMeasuredWidth();
    if (width > 0) {
        Picasso.with(context)
                .load(String.format("http://mybackend.com/image/w_%d,h_%d/%s",
                        holder.image.getMeasuredWidth(), // This returns 0, but i want here a real width
                        holder.image.getMeasuredWidth(),
                        myCollection.get(position).getImagePid()))
                .tag(context)
                .placeholder(R.drawable.placeholder_logo)
                .into(holder.image);
    } else {
        holder.image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                holder.image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Picasso.with(context)
                        .load(String.format("http://mybackend.com/image/w_%d,h_%d/%s",
                                holder.image.getMeasuredWidth(), // This returns 0, but i want here a real width
                                holder.image.getMeasuredWidth(),
                                myCollection.get(position).getImagePid()))
                        .tag(context)
                        .placeholder(R.drawable.placeholder_logo)
                        .into(holder.image);
            }
        });
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28303157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档