简体   繁体   English

自定义视图在触摸事件上反应不佳

[英]Custom View not respond very well on touch events

I have a custom grid which extends ViewGroup and I have the next onLayout and measureChildrenSizes methods: 我有一个扩展了ViewGroup的自定义网格,还有下一个onLayout和measureChildrenSizes方法:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    // .. tileSide calcultion ..

    measureChildrenSizes(tileSide);
    for (int i = 0; i < getChildCount(); i++) {
        Square child = (Square) getChildAt(i);
        int x = child.getColumn();
        int y = child.getRow();
        child.layout(x * tileSide, y * tileSide,
                (x + 1) * tileSide, (y + 1) * tileSide);
    }   
}

private void measureChildrenSizes(int tileSide) {
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        measureChild(child, tileSide, tileSide);
    }
}

The childs (Square) are custom Views which have a custom onDraw method and the onTouch callback method: 子级(正方形)是具有自定义onDraw方法和onTouch回调方法的自定义视图:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    rect.set(0, 0, getWidth(), getHeight());

        gradient.setBounds(rect);
        gradient.draw(canvas);              

        rectangle.setStrokeWidth(0.2f);
        rectangle.setStyle(Style.STROKE);
        rectangle.setColor(getResources().getColor(
                    R.color.puzzle_foreground));
        canvas.drawRect(rect, rectangle);
        }

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        requestFocus();
        changeState();
        return false;
    }
    return super.onTouchEvent(event);
}   

This draw a grid of Squares which side measure is tileSide. 这将绘制一个方格为方格为方的方格网格。 All the Squares works fine except the last column. 除最后一列外,所有Square均正常运行。 Sometimes they don't respond. 有时他们没有回应。

Perhaps I'm looking for in the wrong direction. 也许我在寻找错误的方向。

Edit: in the emulator works fine. 编辑:在模拟器中工作正常。 It fails in real Devices (tested on Sony xperia u, Samsung galaxy Ace, Samsung galaxy mini). 它在实际设备中失败(在Sony xperia u,Samsung galaxy Ace,Samsung galaxy mini上测试)。

Returning false in your MotionEvent.ACTION_DOWN can cause some of the GestureDetector(if that's what you are using) methods to not be called. 在你返回false MotionEvent.ACTION_DOWN可能会导致一些GestureDetector的(如果这是你使用的是什么)不被调用的方法。 Try returning true there and see if it responds better. 尝试在此处返回true,看看它是否响应更好。

This Android Developers page might help you: Making a View Interactive 这个Android开发人员页面可能会帮助您: 使视图互动

Sometimes bad touch responses can be caused by breaking cycle of managing views by Adapter in AdapterView. 有时,很差的触摸响应可能是由于在AdapterView中中断适配器管理视图的周期而引起的。 I have experienced the same issue when I saved 我保存时遇到了同样的问题
reference to views managed by AdapterView. 引用由AdapterView管理的视图。 It can cause bad response to touches. 可能导致对触摸的不良反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM