How to Listen for several LayoutChange calls?

流过昼夜 提交于 2021-02-11 12:15:49

问题


I am building an application that needs to refresh the UI every time view bounds changes.
I tried the view.addOnLayoutChangeListener() but it called a lot of times.

As wrote in the answer it happens because I didn't remove the LayoutChangeListener, But I want to keep it for the next events.

So my question is:
How can I listen for several LayoutChange events (not only for the first one)?

code:

int counter=0;
    view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                  public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                      Toast.makeText(getContext(),++counter+"",Toast.LENGTH_SHORT).show(); //goes to infinity
                  }
             });

I read this answer too, but it requires to create a new subclass.
Can the onSizeChanged for the layout help me in this situation?

来源:https://stackoverflow.com/questions/65831475/how-to-listen-for-several-layoutchange-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!