gpt4 book ai didi

android - 捕获android列表项中的触摸事件并防止滚动

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:01 25 4
gpt4 key购买 nike

我在这里尝试一个疯狂的想法,将自定义控件放在某个 ListView 的项目中。只有当用户按下某个触发点然后他们可以“四处拖动”时,控件才会“激活”。

我的问题是,我可以在 onTouchEvent(...) 中做什么来阻止 ListView 接收事件和滚动。现在我可以触摸并捕获控件,但如果我将手指向上或向下移动太多, ListView 就会接管并开始滚动,那么我的 View 甚至不会收到 ACTION_UP 事件。

这是我当前的 onTouchEvent 代码:

if (e.getAction() == MotionEvent.ACTION_DOWN) {

Log.d("SwipeView", "onTouchEvent - ACTION_DOWN" + e.getX() + " " + e.getY());
int midX = (int)(this.getWidth() / 2);
int midY = (int)(this.getHeight() / 2);

if (Math.abs(e.getX() - midX) < 100 &&
Math.abs(e.getY() - midY) < 100) {

Log.d("SwipeView", "HEY");
setDragActive(true);

}

this.invalidate();
return true;

} else if (e.getAction() == MotionEvent.ACTION_MOVE) {

_current[0] = e.getX();
_current[1] = e.getY();

this.invalidate();
return true;

} else if (e.getAction() == MotionEvent.ACTION_UP) {

_current[0] = 0;
_current[1] = 0;

setDragActive(false);

this.invalidate();
return true;

}

我确定它在某种程度上与事件层次结构有关。

最佳答案

这可能不是您想要的,但可以在您的 Activity 中实现捕获功能。添加

private View capturedView;
public void setCapturedView(View view) { this.capturedView = view); }

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return (this.capturedView != null) ?
this.capturedView.dispatchTouchEvent(event) :
super.dispatchTouchEvent(event);
}

到您的 Activity,然后只需在 ACTION_DOWN 上传递您的 View ,在 ACTION_UP 上传递 null。它不是很漂亮,但是可以用。不过,我确信有一种正确的方法可以做到这一点。

关于android - 捕获android列表项中的触摸事件并防止滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com