Matrix 安卓-如何捕捉触摸事件,如游戏:世界搜索

Matrix 安卓-如何捕捉触摸事件,如游戏:世界搜索,matrix,textview,touch,android-linearlayout,Matrix,Textview,Touch,Android Linearlayout,我在商店里有一款类似WorldSearch的游戏,我尝试使用以下解决方案: 我通过TextView数组创建了矩阵,并在每个TextView上添加了TouchEvent。然后我通过MOTION.UP,MOTION.DOWN捕捉到事件。当我触摸到一个TextView并拖动它时,虽然它想到了其他TextView,但我只能得到第一个TextView的事件。 以下是代码: private void createMetrix(){ // amound of item in a row and co

我在商店里有一款类似WorldSearch的游戏,我尝试使用以下解决方案: 我通过
TextView
数组创建了矩阵,并在每个
TextView
上添加了
TouchEvent
。然后我通过
MOTION.UP
MOTION.DOWN
捕捉到事件。当我触摸到一个
TextView
并拖动它时,虽然它想到了其他
TextView
,但我只能得到第一个
TextView
的事件。 以下是代码:

private void createMetrix(){

    // amound of item in a row and column
    int itemNum = 10;

    // calculate size of item
    ScreenDimension screenDimension = Utilities.getFinalScreenDimension(this);
    int itemSize = screenDimension.screenWidth / 10;

    // mContentLinear has VERTICAL orientation
    // then adding children linear layouts, these layout has HORIZONTAL orientation
    for(int i = 0; i < itemNum; i++) {
        LinearLayout theItemLayout = createHorizontalLayout(itemNum, itemSize, i);
        mContentLinear.addView(theItemLayout);
    }

}

private LinearLayout createHorizontalLayout(int itemNum, int itemSize, int row) {
    LinearLayout theLinear = new LinearLayout(this);
    theLinear.setOrientation(LinearLayout.HORIZONTAL);
    theLinear.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemSize));
    for(int i = 0; i < itemNum; i++){
        // add item metrix into linear layout having HORIZONTAL orientation
        TextView itemTxt = createItemMatrix(itemSize, row, i);
        theLinear.addView(itemTxt);
    }
    return  theLinear;
}
private TextView createItemMatrix(int itemSize, int row, int column) {
    TextView theItemTxt = new TextView(this);
    theItemTxt.setWidth(itemSize);
    theItemTxt.setHeight(itemSize);
    theItemTxt.setTextSize(13);
    theItemTxt.setGravity(Gravity.CENTER);
    theItemTxt.setText("A" + row + "" + column);
    String tag = row + "|" + column;
    theItemTxt.setTag(tag);

    theItemTxt.setOnTouchListener(mOnTouchListener);
    return theItemTxt;
}
// Listener
private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

    private Rect rect;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v == null) return true;
        TextView itemTxt = (TextView)v;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                Log.d("TEXT", "MOVE DOWN: " + itemTxt.getText().toString());
                return true;
            case MotionEvent.ACTION_UP:
                if (rect != null
                        && !rect.contains(v.getLeft() + (int) event.getX(),
                        v.getTop() + (int) event.getY())) {
                    // The motion event was outside of the view, handle this as a non-click event

                    return true;
                }
                // The view was clicked.
                Log.d("TEXT", "MOVE ON: " + itemTxt.getText().toString());
                // TODO: do stuff
                return true;
            case MotionEvent.ACTION_MOVE:
                Log.d("TEXT", "MOVE OUT: " + itemTxt.getText().toString());
                return true;
            default:
                return true;
        }
    }
};
private void createMetrix(){
//行和列中项目的数量
int itemNum=10;
//计算项目的大小
ScreenDimension ScreenDimension=Utilities.getFinalScreenDimension(此);
int itemSize=screenDimension.screenWidth/10;
//mContentLinear具有垂直方向
//然后添加子线性布局,这些布局具有水平方向
for(int i=0;i
有人对这个案子有什么想法吗

谢谢,
Ryan

如果你添加一些代码来展示你的尝试,你会得到更具建设性的答案。@Morne:我添加了我的代码,请告诉我你的想法,非常感谢!