getWidth() 和 getHeight() 在 RecyclerView/Fragment 中返回 0

2024-01-09

我正在调用填充方法RecyclerView在 - 的里面OnCreateView方法在几个片段中,然后它们被定向到填充提要的适配器(取决于context of the Fragment).

目前,我正在向提要上用户上传的图像添加标签。这些标签需要一个x and yvalue(位置),其中是容器的百分比(在上传时设置并保存到 BaaS)。目前,像素到百分比公式工作得很好,但是当我尝试获取容器的尺寸时,我尝试过的每个选项returns 0每次。

RecyclerViewHolderAllFeeds(View view) {
    ...
    this.imageContainer = (RelativeLayout) view
                .findViewById(R.id.image_container);
}

标签片段: (该函数在bind内部调用,将所有标签绑定到上传的图片上)

  float x = containerWidth - Float.parseFloat(model.getXpoints(o)) / 100 * containerWidth;
            float y = containerHeight - Float.parseFloat(model.getYpoints(o)) / 100 * containerHeight;
            Log.e("measuredWidth", "" + containerHeight + "" + containerWidth);
            Log.e("getPointX", "" + model.getXpoints(o) + "" + model.getYpoints(o));
            Log.e("x", "x" + x + "y" + y);
            tag.setX(x);
            tag.setY(y);

我目前在 OnCreateViewHolder 方法中设置了 containerWidth 的值:

RecyclerViewHolderAllFeeds holder = null;
    LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
    if (viewType == 1) {
        // This method will inflate the custom layout and return as viewholde
        ViewGroup mainGroup = (ViewGroup) mInflater.inflate(R.layout.feed_and_search_feed_item_row, viewGroup, false);
        holder =  new RecyclerViewHolderAllFeeds(mainGroup);
        containerWidth = getContainerWidth(holder);
        containerHeight = getContainerHeight(holder);
    } else {
        ViewGroup mainGroup = (ViewGroup) mInflater.inflate(R.layout.progress_item, viewGroup, false);
        holder =  new ProgressViewHolder(mainGroup);
    }
    return  holder;

我也在里面尝试过这种方式onViewAttchedToWindow method:

   @Override
   public void onViewAttachedToWindow(RecyclerViewHolderAllFeeds holder) {
       super.onViewAttachedToWindow(holder);
       containerWidth = getContainerWidth(holder);
       containerHeight = getContainerHeight(holder);
   }

但再一次x and y值等于零。

GetContainerWidth(ViewHolderholder)方法:

 float getContainerWidth(final RecyclerViewHolderAllFeeds h) {
        final int[] width = new int[1];
        final ViewTreeObserver observer = h.imageContainer.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                width[0] = h.imageContainer.getWidth();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    h.imageContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    h.imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });
        return width[0];
    }

同样的逻辑也适用于getContainerHeight(ViewHolder holder)方法。

我还尝试过在树观察器上没有全局侦听器并且在 preDraw 参数上没有侦听器。

observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                height[0] = h.imageContainer.getHeight();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    h.imageContainer.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return false;
            }
        });

Log:

E/测量的宽度和高度:0.0 0.0
E/getPointX&Y: 70.13 88.00
E/x:x 0.0 y 0.0

是否可以解决在其他地方初始化布局并收集信息并将其存储在某个地方的问题?我已经用尽了很多选择。我只是觉得我还没有看够周围的一切Fragments and RecyclerViewsSO 社区内。

任何帮助将不胜感激。


As @Enzokie提到你必须计算你的x and y在你知道你的宽度和高度之后的内部回调imageContainer。所以你需要做的就是移动你的OnGlobalLayoutListener to onBindViewHolder方法,像这样:

@Override
public void onBindViewHolder(RecyclerViewHolderAllFeeds h, int position) {
    ...
    final ViewTreeObserver observer = h.imageContainer.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                h.imageContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                h.imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            int containerWidth = h.imageContainer.getWidth();
            int containerHeight = h.imageContainer.getHeight();
            float x = containerWidth - Float.parseFloat(model.getXpoints(o)) / 100 * containerWidth;
            float y = containerHeight - Float.parseFloat(model.getYpoints(o)) / 100 * containerHeight;
            Log.e("measuredWidth", "" + containerHeight + "" + containerWidth);
            Log.e("getPointX", "" + model.getXpoints(o) + "" + model.getYpoints(o));
            Log.e("x", "x" + x + "y" + y);

            h.tag.setX(x);
            h.tag.setY(y);
            h.tag.requestLayout();
        }
    });
    ...
}

希望有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

getWidth() 和 getHeight() 在 RecyclerView/Fragment 中返回 0 的相关文章

随机推荐