Code example for TableRow

Methods: addViewsetDescendantFocusabilitysetGravity, setClickable, setOnClickListener

0
  private void initTableView(Context context) {
    this.expanded = true;
    this.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    addHeaderRow(context);
    addContentRow(context);
    this.headerRow.setOnClickListener(new OnHeaderClick(this));
    LayoutTransition transition = new LayoutTransition();
    AnimatorSet animAppear = new AnimatorSet();
    animAppear.setDuration(animationDuration).playTogether(
        ObjectAnimator.ofFloat(contentRow, "alpha", 0, 1),
        ObjectAnimator.ofFloat(contentRow, "scaleY", 0, 1));
    transition.setAnimator(LayoutTransition.APPEARING, animAppear);
    AnimatorSet animDisappear = new AnimatorSet();
    animDisappear.setDuration(animationDuration).playTogether(
        ObjectAnimator.ofFloat(contentRow, "alpha", 1, 0),
        ObjectAnimator.ofFloat(contentRow, "scaleY", 1, 0));
    transition.setAnimator(LayoutTransition.DISAPPEARING, animDisappear);
    transition.setStartDelay(LayoutTransition.APPEARING, 0);
    transition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
    this.setLayoutTransition(transition);
  } 
  private boolean measuringOnlyDirectKids = false;
  private boolean isMeasuringOnlyDirectKids() { 
    return measuringOnlyDirectKids;
  } 
  private void setMeasuringOnlyDirectKids(boolean measuringOnlyDirectKids) {
    this.measuringOnlyDirectKids = measuringOnlyDirectKids;
  } 
  private boolean drawingDontMove = false;
  public boolean isDrawingDontMove() { 
    return drawingDontMove;
  } 
  public void setDrawingDontMove(boolean drawingDontMove) {
    this.drawingDontMove = drawingDontMove;
  } 
  @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    boolean m = this.isMeasuringOnlyDirectKids();
    this.setMeasuringOnlyDirectKids(true);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//    if (this.isInEditMode() && !this.isDrawingDontMove()) { 
//      for (int i = 0; i < this.content.getChildCount(); i++) { 
//        this.content.getChildAt(i).offsetLeftAndRight(this.content.getLeft()); 
//        this.content.getChildAt(i).offsetTopAndBottom(this.contentRow.getTop()); 
//      } 
//    } 
    this.setMeasuringOnlyDirectKids(m);
    LayoutTransition transition = this.getLayoutTransition();
    transition.setAnimator(LayoutTransition.CHANGE_APPEARING,
        MarginAnimator.ofMargin(this, MarginAnimator.Margin.BOTTOM, -contentRow.getMeasuredHeight(), 0)
            .setDuration(animationDuration));
    transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
        MarginAnimator.ofMargin(this, MarginAnimator.Margin.BOTTOM, contentRow.getMeasuredHeight(), 0)
            .setDuration(animationDuration));
  } 
  @Override 
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    boolean m = this.isMeasuringOnlyDirectKids();
    this.setMeasuringOnlyDirectKids(true); 
    super.onLayout(changed, l, t, r, b);
    if (this.isInEditMode() && !this.isDrawingDontMove()) {
      for (int i = 0; i < this.content.getChildCount(); i++) {
        this.content.getChildAt(i).offsetLeftAndRight(this.content.getLeft());
        this.content.getChildAt(i).offsetTopAndBottom(this.contentRow.getTop());
        this.content.getChildAt(i).setTranslationX(-this.content.getLeft());
        this.content.getChildAt(i).setTranslationY(-this.contentRow.getTop());
      } 
    } 
    this.setMeasuringOnlyDirectKids(m);
  } 
   
  @Override 
  protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean d = this.isDrawingDontMove();
    this.setDrawingDontMove(true);
    boolean ret = super.drawChild(canvas, child, drawingTime);
    this.setDrawingDontMove(d);
    return ret;
  } 
//  @Override 
//  protected void dispatchDraw(Canvas canvas) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.dispatchDraw(canvas); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  protected boolean getChildStaticTransformation(View child, Transformation t) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    boolean ret = super.getChildStaticTransformation(child, t); 
//    this.measuringOnlyDirectKids = m; 
//    return ret; 
//  } 
//  @Override 
//  public void getFocusedRect(Rect r) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.getFocusedRect(r); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  public boolean getGlobalVisibleRect(Rect r, Point globalOffset) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    boolean ret = super.getGlobalVisibleRect(r, globalOffset); 
//    this.measuringOnlyDirectKids = m; 
//    return ret; 
//  } 
//  @Override 
//  public void getLocationInWindow(int[] location) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.getLocationInWindow(location); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  public void getLocationOnScreen(int[] location) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.getLocationOnScreen(location); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  public void getHitRect(Rect outRect) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.getHitRect(outRect); 
//    this.measuringOnlyDirectKids = m; 
//  } 
   
//  @Override 
//  protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.measureChildren(widthMeasureSpec, heightMeasureSpec); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, 
//      int parentHeightMeasureSpec, int heightUsed) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed); 
//    this.measuringOnlyDirectKids = m; 
//  } 
//  @Override 
//  public void draw(Canvas canvas) { 
//    boolean m = this.measuringOnlyDirectKids; 
//    this.measuringOnlyDirectKids = true; 
//    super.draw(canvas); 
//    this.measuringOnlyDirectKids = m; 
//  } 
  @Override 
  public int getChildCount() { 
    if (this.isMeasuringOnlyDirectKids())
      return super.getChildCount(); 
    int ret = super.getChildCount();
    if (this.content != null) ret += this.content.getChildCount();
    return ret;
  } 
  @Override 
  public View getChildAt(int index) {
    if (this.isMeasuringOnlyDirectKids())
      return super.getChildAt(index);
    if (this.content != null && index >= super.getChildCount())
      return this.content.getChildAt(index - super.getChildCount());
    return super.getChildAt(index);
  } 
  @Override 
  public boolean getChildVisibleRect(View child, Rect r, Point offset) {
    //if (this.measuringOnlyDirectKids || this.content == null) 
      return super.getChildVisibleRect(child, r, offset);
    //return this.content.getChildVisibleRect(child, r, offset); 
  } 
  @Override 
  public void getDrawingRect(Rect outRect) {
    boolean m = this.isMeasuringOnlyDirectKids();
    this.setMeasuringOnlyDirectKids(true);
    super.getDrawingRect(outRect);
    this.setMeasuringOnlyDirectKids(m);
  } 
   
  class OnHeaderClick implements OnClickListener {
    ExpandoGroup table;
    public OnHeaderClick(ExpandoGroup table) {
      this.table = table;
    } 
    public void onClick(View v) {
      if (this.table.expanded) {
        this.table.expando.setImageResource(R.drawable.navigation_expand);
        MarginLayoutParams params = (MarginLayoutParams)this.table.getLayoutParams();
        params.bottomMargin = this.table.contentRow.getMeasuredHeight();
        this.table.getLayoutTransition().hideChild(this.table, this.table.contentRow);
        this.table.contentRow.setVisibility(View.GONE);
      } else { 
        this.table.expando.setImageResource(R.drawable.navigation_collapse);
        this.table.contentRow.setVisibility(View.VISIBLE);
        MarginLayoutParams params = (MarginLayoutParams)this.table.getLayoutParams();
        params.bottomMargin = -this.table.contentRow.getMeasuredHeight();
        this.table.getLayoutTransition().showChild(this.table, this.table.contentRow);
      } 
      this.table.expanded = !this.table.expanded;
    } 
  } 
  private float dipToPx(float dip) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, this.getResources().getDisplayMetrics());
  } 
  private void addHeaderRow(Context context) {
    this.headerRow = new TableRow(context);
    this.headerRow.setClickable(true);
    this.headerRow.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    this.headerRow.setGravity(Gravity.CENTER_VERTICAL);
     
    this.expando = new ImageView(context);
    int eightDip = (int)dipToPx(8);
    TableRow.LayoutParams p = new TableRow.LayoutParams(0);
    p.setMargins(eightDip, eightDip, eightDip, eightDip);
    this.expando.setImageResource(this.getCollapseDrawable());
    this.headerRow.addView(this.expando, p);
     
    this.label = new TextView(context, null, this.getCaptionAppearance());
    this.label.setText(this.getCaption());
    this.headerRow.addView(this.label, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
     
    super.addView(this.headerRow, -1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     
  } 
  private void addContentRow(Context context) {