Skip to content Skip to sidebar Skip to footer

Getwidth() & Getheight() Returning 0 Inside Recyclerview/fragment

I'm calling the populating method for the RecyclerView inside the OnCreateView method in several fragments, they are then directed to the adapter for which populates the feeds (dep

Solution 1:

As @Enzokie mentioned you have to calculate your x and y inside callback after you will know width and height of your imageContainer. So all you need is to move your OnGlobalLayoutListener to onBindViewHolder method, like this:

@OverridepublicvoidonBindViewHolder(RecyclerViewHolderAllFeeds h, int position) {
    ...
    finalViewTreeObserverobserver= h.imageContainer.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                h.imageContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                h.imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            intcontainerWidth= h.imageContainer.getWidth();
            intcontainerHeight= h.imageContainer.getHeight();
            floatx= containerWidth - Float.parseFloat(model.getXpoints(o)) / 100 * containerWidth;
            floaty= 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();
        }
    });
    ...
}

Hope that helps!

Post a Comment for "Getwidth() & Getheight() Returning 0 Inside Recyclerview/fragment"