【问题标题】:Android - basic gesture detection in TableLayoutAndroid - TableLayout 中的基本手势检测
【发布时间】:2023-04-09 00:10:01
【问题描述】:

我是android开发的新手,所以请原谅这篇文章的幼稚。我有一个表格布局,我在其中动态地将文本视图添加到每个单元格。我希望在单元格上添加滑动检测,并根据滑动的单元格执行操作。我尝试在每个单元格上添加一个 onSwipeTouchListener。

TextView txtviewCell = new TextView(getActivity());
TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(60, 60);
txtviewCell.setBackgroundColor(0xfffaf8ef);
txtviewCell.setGravity(Gravity.CENTER);
paramsExample.setMargins(5, 5, 5, 5);
txtviewCell.setLayoutParams(paramsExample);
final int currRow = i;
final int currCol = j;
txtviewCell.setOnTouchListener(new OnSwipeTouchListener(
        getActivity()) {
    public void onSwipeTop() {
        handleSwipe(currRow, currCol);
    }

    public void onSwipeRight() {
        handleSwipe(currRow, currCol);
    }

    public void onSwipeLeft() {
        handleSwipe(currRow, currCol);
    }

    public void onSwipeBottom() {
        handleSwipe(currRow, currCol);
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});
allCells[i][j] = txtviewCell;

现在,当我滑动时,我在 onSwipe 函数中获得了控件,但我没有获得正确的行号和列号。是否有特定的方法可以在 textview 上附加滑动事件并获取该单元格的行号和列号?

【问题讨论】:

    标签: android listener gesture-recognition


    【解决方案1】:

    我猜每个 textview 都有不同的 ID,所以尝试获取 id 并使用它进行控制流。

    【讨论】:

    • 由于我是动态生成文本视图,我找不到为文本视图分配不同 ID 的方法。
    • @user1045047 他们都有相同的文本要显示?
    • 不,不同的盒子有不同的文字。
    • @user1045047 那么,如果您从每个小部件中获取文本并制作一个 switch case 以确定对应于哪个手势呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多