How to handle visibility changes for a custom android view/widget

前端 未结 4 1995
感情败类
感情败类 2021-01-04 05:09

I have written a custom view in android. I need to do some processing when visibility of this view is changed. Is there some listener which is called when visibility of a vi

4条回答
  •  伪装坚强ぢ
    2021-01-04 06:09

    I know how to change the visibility, would like to know if there is a listener which is called when we setVisibility on the view!

    you have to subclass your view/widget and override setVisibility, and register a interface on which you will recive the notification. For instance:

    public class MyView extends View {
    
      public interface MyListener {
        public void onSetVisibilityCalled();
      }
    
      public void registerListener(MyListener myListener) {
        this.mListener = myListener;
      }
    
     public void setVisibility (int visibility) {
       super.setVisibility(visibility);
       if (mListener != null)
         mListener.onSetVisibilityCalled();
     }
    
    }
    

提交回复
热议问题