Android - listener / callback for when the number of changes to "View children"

In Android, can I use / implement a listener that starts a callback when the number of children for a given view changes dynamically? Say, for example, View has 3 immediate children, and one of them is deleted using the code. I need an event to shoot, to say that the number of immediate children has changed.

To clarify, I don’t need to go through the depth in the viewing tree to count children, I’m just interested in counting the number of children at the first level of this view.

Many thanks

+5
source share
2 answers

.

  • . View. Into addView.

  • ViewGroup . addView. .

-

public class MyViewGroup extends LinearLayout {

  // provide constructor implmentation to justify super constructor requirement

  private OnSizeChangeListener callback;

  public void setOnSizeChangeListener(OnSizeChangeListener callback) {
     this.callback = callback;
  }

  @Override
  public void remove(View view) {
    super.remove(view);
    if(callback != null) {
       callback.sizeChanged(getChildCount);
    }
  }   
}
+1

All Articles