gpt4 book ai didi

java - Android Studio 的 onClickListener 问题

转载 作者:行者123 更新时间:2023-11-30 08:08:33 25 4
gpt4 key购买 nike

我一直在尝试构建一个简单的益智应用程序,更多的是为了向自己证明我可以。这是一个简单的 9 block 拼图,其中缺少一 block 。与缺失的图 block 相邻的图 block 可以移动到该槽中。

我尝试做的是使用 9 个 ImageView 的网格布局作为图 block 。每当要移动图 block 时,我都会实例化第十个 ImageView ,并将该图 block 的图像源和位置传递给它。然后,原始图 block 被设置为不可见,并且移动图 block 移动到新位置,此时它将这些数据传递给接收图 block 并变得不可见。

首先,我找到两个相邻 imageView 之间的偏移量:

    int startPosition1[] = new int[2];
topLeft.getLocationOnScreen(startPosition1);
int startPosition2[] = new int[2];
topCenter.getLocationOnScreen(startPosition2);

final int separation = startPosition2[1] - startPosition1[1];

然后,在 OnClickListener 中,我检查移动是否有效并调用我创建的用于执行移动的方法 (moveme):

    topLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int thisLocation[] = new int[2];
v.getLocationOnScreen(thisLocation);

int invisibleLocation[] = new int[2];
findViewById(theInvisible).getLocationOnScreen(invisibleLocation);

if ((thisLocation[0] - invisibleLocation[0]) == 0)
{
if ((thisLocation[1] - invisibleLocation[1]) == separation)
{
Toast.makeText(getApplicationContext(), R.string.boldly ,Toast.LENGTH_LONG).show();
moveMe(thisLocation, "translationY", (0-separation), v);

}
else if((invisibleLocation[1] - thisLocation[1]) == separation)
{
Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
moveMe(thisLocation, "translationY", separation, v);

}
}
else if ((thisLocation[1] - invisibleLocation[1]) == 0) {
if ((thisLocation[0] - invisibleLocation[0]) == separation) {
Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
moveMe(thisLocation, "translationX", (0 - separation), v);


} else if ((invisibleLocation[0] - thisLocation[0]) == separation) {
Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
moveMe(thisLocation, "translationX", separation, v);

}
}
else
{
Toast.makeText(getApplicationContext(), R.string.illogical, Toast.LENGTH_LONG).show();
}

}
});

这是 moveMe 方法:

    void moveMe( int[] thisLocation, String direction, int separation, View v)
{
Log.i(TAG, "Entere mover");
ImageView mover = (ImageView) findViewById(R.id.mover);
ImageView destination = (ImageView) findViewById(getTheInvisible());
mover.setContentDescription(v.getContentDescription());
setView(mover);

mover.setX(thisLocation[0]);
mover.setY(thisLocation[1]);
Log.i(TAG, "relocated mover");

mover.setVisibility(View.VISIBLE);
v.setVisibility(View.INVISIBLE);
v.setClickable(false);
setTheInvisible(v);
Log.i(TAG, "Swapped visibilities");

ObjectAnimator test = ObjectAnimator.ofFloat(mover, direction, separation);
test.setDuration(1000);
test.setRepeatCount(0);
test.start();
Log.i(TAG, "animation complete");

destination.setContentDescription(mover.getContentDescription());
setView(destination);
destination.setVisibility(View.VISIBLE);
destination.setClickable(true);
mover.setVisibility(View.INVISIBLE);
Log.i(TAG, "Swapped views with destination");
}

据我所知,这个方法甚至没有被调用。此外,任何符合调用此方法标准的图 block 也不会发出其 toast 消息。最后,我注意到任何与空白图 block 直接沿 x 或 y 轴设置两个空格的图 block 也不会显示其文本。提前致谢。

最佳答案

好吧,经过近一周的查找,我发现了问题所在。事实证明,GridLayouts 提供了自己的动画。为了启用它们,您所要做的就是将其放入 xml 中的 GridLayout 项目中:

android:animateLayoutChanges="true"

然后只需进行更改并调用layoutAnimator,如下所示:

mover.setLayoutParams(destination.getLayoutParams());
theGrid.startLayoutAnimation();

在此示例中,移动器是一个 ImageView,它临时接收其他 View 的图像并将它们移动到其他网格坐标。 theGrid 是我为网格布局指定的资源 ID 名称。

它不对某些图 block 执行任何操作的问题是我的 if then 语句中的错误。

关于java - Android Studio 的 onClickListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30735349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com