<OnSwipe>
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
指定当用户在布局上滑动时要执行的操作。云计算的速度
目标视图的运动序列和运动会受到
滑动屏幕的速度和方向,具体取决于您使用可选功能
参数。
一个 <Transition>
可以有多个 <OnSwipe>
节点,
<OnSwipe>
,用于指定不同的滑动方向和
在用户执行滑动操作时执行什么操作。
语法
<OnSwipe
motion:touchAnchorId="@id/target_view"
motion:touchAnchorSide="side"
[ motion:dragDirection="direction" ]
[ motion:dragScale="scale" ]
[ motion:maxVelocity="maxVelocity" ]
[ motion:maxAcceleration="maxAcceleration" ]
/>
属性
motion:touchAnchorId
- 因滑动而移动的视图。
motion:touchAnchorSide
- 滑动锚定到的目标视图的一侧。
MotionLayout
会保持
固定的距离。可接受的值
为 "left"
、"right"
、"top"
和 "bottom"
。
motion:dragDirection
- 用户滑动动作的方向。如果设置了该属性,则此
<OnSwipe>
仅适用于朝指定方向的滑动。可接受的值
为 "dragLeft"
、"dragRight"
、"dragUp"
和 "dragDown"
。
motion:dragScale
控制视图相对于滑动长度的移动距离。通过
默认值为 1,表示视图按照滑动的方向移动。
如果 dragScale
小于 1,则视图移动的距离会小于滑动距离。
例如,dragScale
为 0.5 表示如果滑动移动 4 厘米,
目标视图会移动 2 厘米
如果 dragScale
大于 1,则视图移动比滑动距离更远
距离。例如,dragScale
为 1.5 表示如果滑动操作移动了 4 次
cm,目标视图会移动 6 cm。
motion:maxVelocity
目标视图的最大速度。
motion:maxAcceleration
目标视图的最大加速度。
包含于
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# <OnSwipe>\n\nSpecifies the action to perform when the user swipes on the layout. The speed of\nthe motion sequence and the motion of the targeted view are affected by the\nspeed and direction of the swipe, subject to the limits you set with optional\nparameters.\n\nThere can be multiple `\u003cOnSwipe\u003e` nodes for a single `\u003cTransition\u003e`, with each\n`\u003cOnSwipe\u003e` specifying a different swipe direction and a different action to\nperform when the user performs that swipe.\n\nSyntax\n------\n\n```xml\n\u003cOnSwipe\n motion:touchAnchorId=\"@id/target_view\"\n motion:touchAnchorSide=\"side\"\n[ motion:dragDirection=\"direction\" ]\n[ motion:dragScale=\"scale\" ]\n[ motion:maxVelocity=\"maxVelocity\" ]\n[ motion:maxAcceleration=\"maxAcceleration\" ]\n /\u003e\n```\n\nAttributes\n----------\n\n`motion:touchAnchorId`\n: View that is being moved by the swipe.\n\n`motion:touchAnchorSide`\n: Side of the target view that the swipe is anchored to. `MotionLayout` keeps a\n constant distance between the anchor and the user's finger. Acceptable values\n are `\"left\"`, `\"right\"`, `\"top\"`, and `\"bottom\"`.\n\n`motion:dragDirection`\n: Direction of the user's swipe motion. If this attribute is set, this\n `\u003cOnSwipe\u003e` only applies to swipes in the specified direction. Acceptable values\n are `\"dragLeft\"`, `\"dragRight\"`, `\"dragUp\"`, and `\"dragDown\"`.\n\n`motion:dragScale`\n\n: Controls the distance the view moves relative to the length of the swipe. The\n default value is 1, indicating that the view moves as far as the swipe does.\n If `dragScale` is less than 1, the view moves less than the swipe distance.\n For example, a `dragScale` of 0.5 means that if the swipe moves 4 cm, the\n target view moves 2 cm.\n\n If `dragScale` is greater than 1, the view moves farther than the swipe\n distance. For example, a `dragScale` of 1.5 means that if the swipe moves 4\n cm, the target view moves 6 cm.\n\n`motion:maxVelocity`\n\n: Maximum velocity of the target view.\n\n`motion:maxAcceleration`\n\n: Maximum acceleration of the target view.\n\nContained in\n------------\n\n- [`\u003cTransition\u003e`](/training/constraint-layout/motionlayout/ref/transition)"]]