2016-11-11 83 views
0

我在几个片段中调用OnCreateView方法中RecyclerView方法的RecyclerView的填充方法,然后将它们定向到为其填充提要的适配器(取决于Fragmentcontext)。getWidth()&getHeight()在RecyclerView/Fragment中返回0

当前,我正在向Feed上的用户上传的图片添加标签。这些标签需要一个xy值(位置),其中是容器的百分比(在上载时将其设置并保存到BaaS)。目前,百分比公式的像素工作正常,但是当我尝试抓取容器的尺寸时,每次我都尝试过returns 0

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

标签段: (此功能被称为绑定内,结合所有的标签上传的图片)

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); 

我设置containerWidth值在OnCreateViewHolder方法的那一刻:

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方法中这样说:

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

但是再次xy值等于零。

GetContainerWidth(ViewHolder持有者)方法:

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; 
      } 
     }); 

日志:

E/measuredWidth可以&高度:0.0 0.0
E/getPointX & Y:70.13 88.00
E/X:X 0.0是0.0

关于在其他地方初始化布局并收集信息并将其存储在某个地方,是否可以解决一些问题?我用尽了很多选择。我只是觉得我在SO社区的FragmentsRecyclerViews附近没有看到足够的东西。

任何帮助将不胜感激。

+0

'addOnGlobalLayoutListener'不以线性执行,您需要等待它的回调。 – Enzokie

+0

但是,当我需要高度和宽度(在ViewHolder已被绑定为前几个项目之后)时,回调时间太迟,您会推荐其他任何东西吗? – BIW

+0

你需要在回调中执行你的数学逻辑,那也是唯一可以操纵视图的地方(如果有必要的话)。 – Enzokie

回答

2

由于@Enzokie提到你算算你的xy内回调,你会知道宽度和高度的imageContainer后。因此,所有你需要的是将你的OnGlobalLayoutListeneronBindViewHolder方法,像这样:

@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(); 
     } 
    }); 
    ... 
} 

希望帮助!

+0

感谢您抽出时间,我一定会测试一下。 – BIW

+0

完美地工作,谢谢。 – BIW

+0

不客气! – rom4ek

相关问题