简体   繁体   English

使用触摸事件移动视图

[英]Moving view using touch events

I want to make an application with sliding navigation, ie user can move views horizontally, sliding them in or out. 我想用滑动导航制作一个应用程序,即用户可以水平移动视图,将其滑动进出。 Currently I have implemented my custom ViewGroup with my layout and measurement settings (overridden onLayout and onMeasure). 目前,我已经使用布局和测量设置(覆盖了onLayout和onMeasure)实现了自定义ViewGroup Also I have implemented OnTouchListener calculating difX from starting event point to end point. 我还实现了OnTouchListener计算从开始事件点到结束点的difX。

But I dont know how to apply this difX to my views to change their position. 但是我不知道如何将此difX应用于我的视图以更改其位置。

The easiest way would be to use setTranslationX() method to change view's position according to delta X value. 最简单的方法是使用setTranslationX()方法根据增量X值更改视图的位置。 You can do it in onTouchEvent() . 您可以在onTouchEvent() Do not forget to call invalidate() to force re-draw. 不要忘记调用invalidate()来强制重画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM