gpt4 book ai didi

Android:打开和关闭 ListView 滚动

转载 作者:行者123 更新时间:2023-11-29 21:10:42 24 4
gpt4 key购买 nike

我有一个 ListView,其中有一个签名捕获控件。当用户按下签名捕获控件时,我需要阻止 ListView 滚动——现在发生的情况是捕获控件上的水平笔划起作用,但垂直笔划绘制一条小线,然后开始滚动 ListView。

我找不到打开和关闭 ListView 滚动的简单方法。 StackOverflow 上有一些示例,但很少有人接受答案,而且我已经尝试了很多看起来很有希望但没有成功的解决方案。我觉得可能有一种方法可以告诉签名捕获组件拦截触摸事件,而不是将它们进一步传递到事件链中,但我不知道那是什么。

我的签名捕获是在这里找到的版本的略微修改版本:http://www.mysamplecode.com/2011/11/android-capture-signature-using-canvas.html

功能代码都是一样的。

参见 http://imgur.com/WbfgTkj对于一个SS的问题。以绿色突出显示的区域是签名捕获区域。请注意,它位于 ListView 的底部,必须滚动才能到达它。

最佳答案

好吧,我想通了;我想为下一个偶然发现这个问题的人留下答案是公平的。我提前向 Java 纯粹主义者道歉,我像 C# 一样编写 Java。我也在摸索这个 Android 的东西。

解决方案是创建自定义 ListView。它的神奇之处在于它可以选择打开和关闭触摸事件调度(特别是 ACTION_MOVE,它会滚动)。这是代码:

public class UnScrollingListView extends ListView {

public UnScrollingListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
public UnScrollingListView(Context context, AttributeSet attrs) { super(context, attrs); }
public UnScrollingListView(Context context) { super(context); }

public boolean DisableTouchEventScroll = false;

protected boolean DispatchMoveEventInsteadOfConsumingIt = false;
protected View DispatchTarget = null;

public void ResetToNormalListViewBehavior() {
DisableTouchEventScroll = false;
DispatchMoveEventInsteadOfConsumingIt = false;
DispatchTarget = null;
}

public void SetUpDispatch(View v) {
DisableTouchEventScroll = true;
DispatchMoveEventInsteadOfConsumingIt = true;
DispatchTarget = v;
}

protected static float[] GetRelativeCoordinates(View innerView, MotionEvent e) {
int[] screenCoords = new int [2]; //not sure if I have to preallocate this or not.
innerView.getLocationOnScreen(screenCoords);
return new float[] {
e.getRawX() - screenCoords[0],
e.getRawY() - screenCoords[1]};
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(DispatchMoveEventInsteadOfConsumingIt && DispatchTarget != null) {
//convert coordinate systems
float[] newCoords = GetRelativeCoordinates(DispatchTarget, ev);
ev.setLocation(newCoords[0], newCoords[1]);

DispatchTarget.onTouchEvent(ev);
return true;
}

if(DisableTouchEventScroll && ev.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}

return super.dispatchTouchEvent(ev);
}
}

默认情况下,这就像一个普通的 ListView 。您可以调用 SetUpDispatch(View) 来告诉它停止滚动 ListView 并将所有 ACTION_MOVE 事件分派(dispatch)到特定 View 。然后您可以调用 ResetToNormalListViewBehavior() 使其再次开始滚动。使用我原始帖子中链接的签名捕获代码,您需要做的就是在 onTouchEvent 中更改以下内容:

switch (event.getAction()) 
{
case MotionEvent.ACTION_DOWN:
...
listView.SetUpDispatch(this);
return true;

case MotionEvent.ACTION_UP:
listView.ResetToNormalListViewBehavior();

case MotionEvent.ACTION_MOVE:
...

不确定是否有人会遇到这个问题,但它似乎有一些通用的应用程序,如果你读过这篇文章,祝你好运。

关于Android:打开和关闭 ListView 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970331/

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