【发布时间】:2017-12-17 15:30:28
【问题描述】:
我有一个 LinearLayout,里面有五个视图,它们形成一个 1x5 的网格。
此 LinearLayout 将被拖动。
我目前正在为五个视图中的每一个设置 onTouchListeners,检查拖动来自五个视图中的哪一个,并在 LinearLayout 上调用 motionEvent.getAction()。
这是不完美的,因为 LinearLayout 是通过它的中心而不是拖动开始点来拖动的。
有什么方法可以在motionEvent开始时转换LinearLayout,以便拖动时源自的视图位于手指下方?
在 LinearLayout 内的五个 View 中的每一个上设置的 MyTouchListener
private final class MyTouchListener implements View.OnTouchListener {
//public boolean onTouch(View view, MotionEvent motionEvent) {
public boolean onTouch(View view, MotionEvent motionEvent) {
LinearLayout ship = (LinearLayout)view.getParent();
dragCellIndex = ship.indexOfChild(view);
dragCellTotal = ship.getChildCount();
view = ship;
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
【问题讨论】:
-
你尝试了吗?写了一些代码?
-
我已将我正在使用的触摸监听器添加到原始帖子中。我已经尝试检查LinearLayout中有多少个单元格位于拖动原点的左侧和右侧,以及左侧的setTranslationX(cell.length()* -1)和右侧的setTranslationX(cell.length())。这并没有改变任何东西。
标签: android android-layout android-animation