我正在调用填充方法RecyclerView
在 - 的里面OnCreateView
方法在几个片段中,然后它们被定向到填充提要的适配器(取决于context
of the Fragment
).
目前,我正在向提要上用户上传的图像添加标签。这些标签需要一个x
and y
value(位置),其中是容器的百分比(在上传时设置并保存到 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 RecyclerViews
SO 社区内。
任何帮助将不胜感激。