使用2D滚动的Android奇怪的onClick()行为

时间:2018-02-11 04:53:21

标签: android scroll onclick

我写过自己的"扫雷"试图让它尽可能忠实于最初的微软游戏。

为了创建一个可变大小的雷区,我的XML有一个(垂直)TableLayout嵌套在水平和垂直ScrollViews中。我使用TableRows填充TableLayout,我在其中添加了表示每行中雷区方块的ImageViews。我给每个ImageView一个代表其行/列的id。我的onClick()处理程序将id转换为行/列并处理它。它工作正常,除了滚动雷区一次只能在一个维度上完成。

我发现William Kwan(https://www.youtube.com/watch?v=Y-q6eWFyS54)在YouTube上发布了一种非常好的2D滚动技术。我将它合并到我的应用程序中,它滚动得非常好。但是当我水平滚动时(而不是我垂直滚动),它产生了一个奇怪的onClick()结果。

在新的实现中,当我水平滚动并单击(触摸)一个正方形时,onClick()会收到一个对应的ID,而不是我点击的方块,但是如果我有的话,那就是那个位置的广场不滚动。例如,如果在滚动之前,我单击坐标为(1,3)的正方形,则onClick会收到一个ID为(1,3)的ImageView。但是,如果我滚动对应于左边2个方格的距离,并且我点击屏幕上的相同位置(应该代表坐标(1,5)处的ImageView),则onClick返回其id对应的ImageView(1,3) )。就好像滚动没有发生一样。

我尝试使用getScrollX()进行规避(像素数 OuterHorizo​​ntalScrollView已滚动)和getWidth()(" square"的宽度)来计算" square"已滚动,并调整(x,y)坐标中的y。但是当滚动条接近"半个方形"时,结果有时会被一个关闭。

有什么建议吗?

谢谢。

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/fullscreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/rMines100"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />

            <ImageView
                android:id="@+id/rMines10"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />

            <ImageView
                android:id="@+id/rMines1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />

            <ImageButton
                android:id="@+id/smileyButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight="3"
                android:paddingLeft="40dp" />

            <ImageButton
                android:id="@+id/flagOrMineButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.5"
                android:text="New Button" />

            <ImageView
                android:id="@+id/eTime100"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />

            <ImageView
                android:id="@+id/eTime10"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />

            <ImageView
                android:id="@+id/eTime1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/digit8" />m
        </LinearLayout>


        <com.mpa.minesweeper.OuterHorizontalScrollView
            android:id="@+id/horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9">

            <ScrollView
                android:id="@+id/vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableLayout
                    android:id="@+id/tableLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"></TableLayout>

            </ScrollView>
        </com.mpa.minesweeper.OuterHorizontalScrollView>


    </LinearLayout>
 </layout>

OuterHorizo​​ntalScrollView类:

package com.mpa.minesweeper;

        import android.content.Context;
        import android.util.AttributeSet;
        import android.view.MotionEvent;
        import android.widget.HorizontalScrollView;
        import android.widget.ScrollView;

/**
 * Created by willi on 2017-06-08.
 */

public class OuterHorizontalScrollView extends HorizontalScrollView {
    public ScrollView scrollView;

    public OuterHorizontalScrollView(Context context) {
        super(context);
    }

    public OuterHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OuterHorizontalScrollView(Context context, AttributeSet attrs, 
int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        super.onInterceptTouchEvent(ev);
        scrollView.onInterceptTouchEvent(ev);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        scrollView.dispatchTouchEvent(ev);
        return true;
    }
}

0 个答案:

没有答案
相关问题