2015-06-23 3 views
0

Я пытаюсь скрыть ребенка во время onLayout. Проблема является postInvalidate не отзывает OnLayout после я скрыть ребенкуСкрыть ребенка во время onLayout

public class EllipseFlowLayout extends ViewGroup { 
    private int paddingHorizontal; 
    private int paddingVertical; 
    private int mMaxLines = -1; 
    private EllipseView mEllipse; 
    private View ellipseView; 
    private int numHidden; 
    private boolean mEllipseRemoved = true; 

    public EllipseFlowLayout(Context context) { 
     super(context); 
     init(); 
    } 

    public void setMaxLines(int max) { 
     mMaxLines = max; 
    } 

    public EllipseFlowLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public EllipseFlowLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     paddingHorizontal = getResources().getDimensionPixelSize(R.dimen.flowlayout_horizontal_padding); 
     paddingVertical = getResources().getDimensionPixelSize(R.dimen.flowlayout_vertical_padding); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int childLeft = getPaddingLeft(); 
     int childTop = getPaddingTop(); 
     int lineHeight = 0; 
     int numLines = 1; 
     // 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout 
     int myWidth = resolveSize(MeasureSpec.EXACTLY, widthMeasureSpec); 
     int wantedHeight = 0; 
     for (int i = 0; i < getChildCount(); i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() == View.GONE || child.getTag() == GONE) { 
       continue; 
      } 
      // let the child measure itself 
      child.measure(
        getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width), 
        getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height)); 
      int childWidth = child.getMeasuredWidth(); 
      int childHeight = child.getMeasuredHeight(); 



      // lineheight is the height of current line, should be the height of the heightest view 
      lineHeight = Math.max(childHeight, lineHeight); 
      if (childWidth + childLeft + getPaddingRight() > myWidth) { 
       // wrap this line 
       childLeft = getPaddingLeft(); 
       childTop += paddingVertical + lineHeight; 
       lineHeight = childHeight; 
       numLines++; 
       Log.d("TEST", "measure" + numLines); 
      } 
      childLeft += childWidth + paddingHorizontal; 
     } 



     wantedHeight += childTop + lineHeight + getPaddingBottom(); 
     setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); 
    } 

    @Override 
    public void addView(View child) { 
     //super.addView(child); 
     if (mEllipseRemoved) { 
      addEllipseView(); 
     } 
     super.addView(child, getChildCount() - 1); 
    } 

    public void setEllipseView(EllipseView ellipse) { 
     mEllipse = ellipse; 
     ellipseView = mEllipse.getView(); 
    } 

    @Override 
    public void removeAllViews() { 
     mEllipseRemoved = true; 
     super.removeAllViews(); 
    } 

    private void addEllipseView() { 
     mEllipseRemoved = false; 
     if (ellipseView.getParent() != null) { 
      ((ViewGroup) ellipseView.getParent()).removeView(ellipseView); 
     } 
     ellipseView.setVisibility(View.GONE); 
     addView(ellipseView); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     int childLeft = getPaddingLeft(); 
     int childTop = getPaddingTop(); 
     int lineHeight = 0; 
     int myWidth = right - left; 
     int lineNum = 1; 
     for (int i = 0; i < getChildCount(); i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() == View.GONE) { 
       continue; 
      } 
      int childWidth = child.getMeasuredWidth(); 
      int childHeight = child.getMeasuredHeight(); 
      lineHeight = Math.max(childHeight, lineHeight); 
      if (childWidth + childLeft + getPaddingRight() > myWidth) { 
       childLeft = getPaddingLeft(); 
       childTop += paddingVertical + lineHeight; 
       lineHeight = childHeight; 
       lineNum++; 
       Log.d("TEST", "layout" + lineNum); 
      } 


      child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); 
      childLeft += childWidth + paddingHorizontal; 

     } 
     if (lineNum > mMaxLines) { 
//   if (ellipseView.getVisibility() == View.GONE) { 
//    ellipseView.setVisibility(VISIBLE); 
//   } 
      //Skip the last view which is the hidden one 
      int at = numHidden; 
      if (at < getChildCount() && getChildAt(at) != null) { 
       View child = getChildAt(at); 
       numHidden++; 
       if (child.getVisibility() == View.VISIBLE) { 
        child.setVisibility(View.GONE); 
        child.setTag(GONE); 
        requestLayout(); 
       } 

      } 
      if (mEllipse.getCount() != numHidden) { 
       mEllipse.setEllipseSize(numHidden); 
      } 
     } 
    } 

    /** 
    * Was removed so semi untested but leaving for possible use later 
    * 
    * @param max max children 
    */ 
    public void setMaxChildren(int max) { 
    } 
} 

ответ

0

Скрыть точку в OnDraw забудьте позвонить this.setWillNotDraw (ложь);

Смежные вопросы