final ImageView imageview_array[] = {Tapimage1, Tapimage2, Tapimage3, Tapimage4, Tapimage5, Tapimage6, Tapimage7, Tapimage8, Tapimage9, Tapimage10, Tapimage11, Tapimage12, Tapimage13, Tapimage14, Tapimage15};
final int[] images = {R.drawable.circle_01, R.drawable.square_01, R.drawable.triangle_01,
R.drawable.circle_02, R.drawable.square_02, R.drawable.triangle_02,
R.drawable.circle_03, R.drawable.square_03, R.drawable.triangle_03,
R.drawable.circle_04, R.drawable.square_04, R.drawable.triangle_04,
R.drawable.circle_05, R.drawable.square_05, R.drawable.triangle_05};
final int[] image_id = {R.id.tap_cir_image1, R.id.tap_cir_image2, R.id.tap_cir_image3,
R.id.tap_cir_image4, R.id.tap_cir_image5, R.id.tap_cir_image6,
R.id.tap_cir_image7, R.id.tap_cir_image8, R.id.tap_cir_image9,
R.id.tap_cir_image10, R.id.tap_cir_image11, R.id.tap_cir_image12,
R.id.tap_cir_image13, R.id.tap_cir_image14, R.id.tap_cir_image15};
final int[] circles_array = {1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0};
int[] image_index = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
List<Integer> dataList = new ArrayList<Integer>();
for (int i = 0; i < 15; i++) {
dataList.add(i);
}
Collections.shuffle(dataList);
shuffle_array = new int[dataList.size()];
for (int i = 0; i < dataList.size(); i++) {
shuffle_array[i] = dataList.get(i);
}
for (int i = 0; i < shuffle_array.length; i++) {
System.out.println(shuffle_array[i]);
}
Log.e(" shuffle array", "arr: " + Arrays.toString(shuffle_array));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(height, width);
layoutParams.rightMargin = rightMargin;
layoutParams.gravity = Gravity.CENTER;
for (i = 0; i < shuffle_array.length; i++) {
temp = shuffle_array[i];
imageview_array[i].setBackgroundResource(images[temp]);
imageview_array[i].setId(i);
Log.e("temp", "" + temp);
imageview_array[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(circles_array[temp] == 1 )
{
imageview_array[temp].setVisibility(View.GONE);
count++;
}
}
我有15个图像和3个线性布局。我想在每个布局中排列5个图像,每次用户访问该页面时都会随机生成。
如何为随机图像和相应的点击事件设置ID?
例如,我有15张图像
---&GT; 5(圆圈)
---&GT; 5(三角形)
---&GT; 5(正方形)
所有这些都是随机排列的3种布局,我只想点击圈子。
[我想要的图片]
答案 0 :(得分:0)
这有点模糊,但如果我猜你的目标是正确的,你应该分配你的观点ID,而是随机化他们的drawable,
//pseudocode because I have no idea how you want to do this
class MyPacket{
//local class,lazy way of packaging all your views with an appropriate listener
int id;//drawable id
//pass in all your ids of a certain type with whatever listener type you want
View.onClickListener listener
}
//change your int[] to some MyPacket collection, then for each of your views
ImageView somename= (ImageView) findViewbyId(someID);
somename.setDrawable(mPacketCollection.getFirst().id));
somename.setOnClickListener(mPacketCollection.getFirst().listener);
mPacketCollection.remove(0);
//repeat for each view
这是一个很糟糕的例子,但希望它有所帮助。当你实际上并不关心它们时,将意见本身随机化是没有意义的。