我正在尝试制作益智游戏。我想使用setTag()/getTag()
来确定哪个图块(ImageView
)中的图像,并在用户单击一个图块然后单击另一个图块时交换它们。
显然,在使用Glide时,您不能使用setTag()/getTag()
-You must not call setTag() on a view Glide is targeting
是否有setTag()/getTag()
替代方案或最佳解决方案? --
滑行新手顺便说一句。
谢谢您的时间。
-这是适配器。 image.setTag()
在这里是非法的-
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// references to drawables
private int[] mThumbIds = {
R.drawable.puzzle_0_0,
R.drawable.puzzle_0_1,
R.drawable.puzzle_0_2,
R.drawable.puzzle_0_3,
R.drawable.puzzle_1_0,
R.drawable.puzzle_1_1,
R.drawable.puzzle_1_2,
R.drawable.puzzle_1_3,
R.drawable.puzzle_2_0,
R.drawable.puzzle_2_1,
R.drawable.puzzle_2_2,
R.drawable.puzzle_2_3,
R.drawable.puzzle_3_0,
R.drawable.puzzle_3_1,
R.drawable.puzzle_3_2,
R.drawable.puzzle_3_3,
};
public ImageAdapter(Context c) {
mContext = c;
shuffleArray(mThumbIds);
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image;
if (convertView == null) {
image = new ImageView(mContext);
image.setAdjustViewBounds(true);
}
else {
image = (ImageView) convertView;
}
Glide.with(mContext).load(mThumbIds[position]).into(image);
image.setTag(mThumbIds[position]);
return image;
}
private static void shuffleArray(int[] a) {
Random rng = new Random();
for (int i = a.length - 1; i > 0; i--) {
int index = rng.nextInt(i + 1);
// Simple swap
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
}
-这是getView()
-
public class MainActivity extends AppCompatActivity {
// references to drawables
private Integer[] mThumbIds = {
R.drawable.puzzle_0_0,
R.drawable.puzzle_0_1,
R.drawable.puzzle_0_2,
R.drawable.puzzle_0_3,
R.drawable.puzzle_1_0,
R.drawable.puzzle_1_1,
R.drawable.puzzle_1_2,
R.drawable.puzzle_1_3,
R.drawable.puzzle_2_0,
R.drawable.puzzle_2_1,
R.drawable.puzzle_2_2,
R.drawable.puzzle_2_3,
R.drawable.puzzle_3_0,
R.drawable.puzzle_3_1,
R.drawable.puzzle_3_2,
R.drawable.puzzle_3_3,
};
public GridView gridView;
private boolean swapping = false;
private int firstTilePosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("-MyLog-", view.getTag().toString());
if (!swapping) {
firstTilePosition = position;
swapping = true;
}
else {
ImageView firstTile = (ImageView) parent.getChildAt(firstTilePosition);
ImageView secondTile = (ImageView) view;
int firstTag = (int) firstTile.getTag();
int secondTag = (int) secondTile.getTag();
Glide.with(MainActivity.this).load(secondTag).into(firstTile);
Glide.with(MainActivity.this).load(firstTag).into(secondTile);
firstTile.setTag(secondTag);
secondTile.setTag(firstTag);
// Replace with AlertDialog
if ( gameWon(parent) )
Toast.makeText(MainActivity.this, "Game Won !!!", Toast.LENGTH_LONG).show();
swapping = false;
}
}
});
}
private boolean gameWon(AdapterView<?> parent) {
for (int i = 0; i < parent.getChildCount(); i++) {
if ( ((int) parent.getChildAt(i).getTag()) != mThumbIds[i] )
return false;
}
return true;
}
}
答案 0 :(得分:0)
您可以为Glide保留默认标签,并自己使用基于密钥的标签。
myButton.setTag(100 /* or any other integer key except 0 */, someValue);