【发布时间】:2015-11-19 09:45:41
【问题描述】:
我正在尝试为棒棒糖前版本创建棒棒糖工具栏图标的效果。
我编写了一个扩展 LinearLayout 的类,并在 dispatchTouchEvent 中检查哪个孩子更接近触摸位置并将该事件仅发送给该孩子,以便它忽略孩子的重叠区域和 z-index:
public class OverlappingLayout extends LinearLayout {
public OverlappingLayout(Context context) {
super(context);
}
public OverlappingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
int lastAction = 0;
View lastView = null;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(lastView != null){
lastView.dispatchTouchEvent(ev);
if(ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP)
lastView = null;
return true;
}
float min = 99999999f;
int minIndex = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
float centerX = child.getX() + child.getWidth() / 2;
float centerY = child.getY() + child.getHeight() / 2;
float dis = (float) Math.sqrt(
(ev.getX() - centerX) * (ev.getX() - centerX) +
(ev.getY() - centerY) * (ev.getY() - centerY)
);
if(dis < min) {
min = dis;
minIndex = i;
}
}
getChildAt(minIndex).setClickable(true);
getChildAt(minIndex).dispatchTouchEvent(ev);
lastView = getChildAt(minIndex);
lastAction = ev.getAction();
lastView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//never works for the last child, or the right half of the second child
return false;
}
});
return true;
}
}
但问题是代码在布局的前半部分工作正常,而在后半部分不起作用。
代码在调试模式下工作,并找到最近的孩子并将事件传递给它,但孩子的drawable的按下状态不起作用。
我也将它更改为RelativeLayout,但仍然有同样的问题。
【问题讨论】:
-
您在布局中尝试过 blockDescendentsFocusability 吗?
-
没有,我也不知道怎么用!
-
我认为情况并非如此。如果是这样的话,就不可能有任何视图是可触摸的。
-
没错,困扰我的问题是它适用于布局的后半部分!!!
标签: android android-linearlayout children overlapping touch-event