getWidth()& getHeight()在RecyclerView/Fragment中返回0 [英] getWidth() & getHeight() returning 0 inside RecyclerView/Fragment
问题描述
我在OnCreateView
方法内的几个片段中调用RecyclerView
的填充方法,然后将它们定向到填充提要的适配器(取决于Fragment
的context
) .
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 (depending on the context
of the Fragment
).
目前,我正在将标签添加到Feed中用户上传的图片中.这些标签需要x
和y
值(位置),其中是容器的百分比(在上载时设置并保存到BaaS).目前,像素到百分比的公式非常适用,但是当我尝试获取容器的尺寸时,我每次都尝试过returns 0
的每个选项.
At the minute, I am adding tags to an uploaded image from a user on the feed. Those tags require an x
and y
value (position), in which is a percentage of the container (which is set and saved to a BaaS when they upload). At the moment, the pixel to percentage formula works perfectly but when I try and grab the dimensions of the container, every option I have tried returns 0
every time.
RecyclerViewHolderAllFeeds(View view) {
...
this.imageContainer = (RelativeLayout) view
.findViewById(R.id.image_container);
}
代码段: (此函数在绑定中被调用,可将所有标签绑定到上传的图像上)
Tag Snippet: (This function is called inside the bind, to bind all the tags to the uploaded image)
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的值:
I set the values for containerWidth at the moment in the OnCreateViewHolder method:
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
方法中尝试过这种方式:
I have also tried this way inside the onViewAttchedToWindow
method:
@Override
public void onViewAttachedToWindow(RecyclerViewHolderAllFeeds holder) {
super.onViewAttachedToWindow(holder);
containerWidth = getContainerWidth(holder);
containerHeight = getContainerHeight(holder);
}
但是x
和y
的值再次等于零.
But again x
and y
values equate to zero.
GetContainerWidth(ViewHolderholder)方法:
GetContainerWidth(ViewHolder holder) method:
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)
方法.
the same logic is applied to the getContainerHeight(ViewHolder holder)
method.
我也尝试过在树观察器上不使用全局侦听器,而在preDraw参数上使用侦听器的情况下进行尝试.
I have also tried it without a global listener on the tree observer and with a listener on the preDraw parameters.
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;
}
});
日志:
E/measuredWidth& Height:0.0 0.0
E/getPointX& Y:70.13 88.00
E/x:x 0.0 y 0.0
E/measuredWidth&Height: 0.0 0.0
E/getPointX&Y: 70.13 88.00
E/x: x 0.0 y 0.0
在其他地方初始化布局并收集信息并将其存储在某个地方是否可以解决?我已经用尽了很多选择.我只是觉得我对SO社区中的Fragments
和RecyclerViews
知之甚少.
Could there be a work around in regards to initializing the layout somewhere else and gathering the information and storing it somewhere? I've exhausted plenty of options. I just feel I haven't seen enough around Fragments
and RecyclerViews
within the SO community.
任何帮助将不胜感激.
Any help will be appreciated.
推荐答案
正如 @Enzokie 所述,您必须在知道回调函数的宽度和高度之后在内部回调中计算x
和y
您的imageContainer
.因此,您所需要做的就是将OnGlobalLayoutListener
移至onBindViewHolder
方法,如下所示:
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:
@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();
}
});
...
}
希望有帮助!
这篇关于getWidth()& getHeight()在RecyclerView/Fragment中返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!