【问题标题】:Getting the position of 2D ImageButton ArrayList获取 2D ImageButton ArrayList 的位置
【发布时间】:2019-07-18 09:56:36
【问题描述】:

我有一个名为 ArrayList<ArrayList<ImageButton>> boardImageButtons 的全局 2D ArrayList。

在方法onClick(View view) 中,我有一个名为((ImageButton) view) btn 的单击图像按钮。

我想获得该对象在 2D ArrayList 中的位置(i & j)。

我尽量避免运行 throw 所有 2D ArrayList,比较对象并获取 equals 对象的 ij

你有更好的建议吗?

有没有办法通过onClick(View view) 传递额外的变量?

【问题讨论】:

  • 您可以创建一个Map,将您的按钮标记到位置。类似Map<ImageButton, MyCustomXYPair>

标签: java android imagebutton


【解决方案1】:

在您的应用开始时创建一个HashMap

class Pair {
    int x, y;
    Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}


Map<ImageButton, Pair> map = new HashMap<?>();

通过使用一些循环或类似逻辑放置所有值来初始化地图

map.put(btn1, new Pair(1, 2));
..
..

在检索位置时,使用

Pair myPair = map.get(view);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2018-02-25
    • 2017-06-20
    • 1970-01-01
    相关资源
    最近更新 更多