Listen on a View if the View change its XY position

痴心易碎 提交于 2021-01-27 04:45:16

问题


How do I listen on a View to obtain its XY coordination if it is moving or its XY position change? The View is not necessary to be dragged (or by user touch event), it may change if the orientation change or pushed (upward) by the showing keyboard or etc. I want to know its updated XY coordination when it changes, how can I do that?


回答1:


You may do something like this

final View activityRootView = findViewById(R.id.mainLayout);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {
                        findViewById(R.id.textView).getX();
                        findViewById(R.id.textView).getY();

                    }
                });

Hope this helps.



来源:https://stackoverflow.com/questions/20461784/listen-on-a-view-if-the-view-change-its-xy-position

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