[英]Possible to iterate over a column or row of GridLayout contents in android?
我正在制作井字游戲,希望使邏輯重復性降低。 我目前正在通過以下操作檢查每個點擊位置的所有可能的獲勝條件:
public static boolean isCompleted(int position, ImageView[] blocks) {
GameLogic.blocks = blocks;
boolean isComplete = false;
switch (position) {
case 1:
isComplete = areSameInSet(1, 2, 3) ||
areSameInSet(1, 4, 7) ||
areSameInSet(1, 5, 9);
break;
並將它們發送到此方法,該方法檢查一個數字值,單擊該值將設置為0表示圓圈,1表示為十字。
private static boolean areSameInSet(int first, int second, int third) {
boolean value = blocks[first - 1].getId() == blocks[second - 1].getId() &&
blocks[second - 1].getId() == blocks[third - 1].getId();
是否可以查看此gridview的整個行或列,而不是每次都需要3個不同參數的自定義方法?
我嘗試查看TableView來查看這是否是更好的選擇,但是我也無法查看該路線中的任何方式。
理想情況下,我可以運行諸如checkSum(row,column)之類的方法,該方法求和以檢查3或-3(一個玩家用1表示,一個玩家用-1表示),對角線有一些特殊條件。
編輯:我不完全確定您的意思,但是網格是靜態的,並且每個imageview占據一個位置,並且不會移動。
<GridLayout
android:id="@+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/colorPrimaryDark"
android:columnCount="3"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
接下來是九個嵌套的ImageView,其中之一像這樣:
<ImageView
android:id="@+id/block1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_row="0"
android:layout_column="0"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
如果列號是固定的,則可以在getView(position)
執行此操作。 您將獲得該職位。
您可以在鏈接中找到自定義網格視圖的示例
自定義適配器
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] osNameList, int[] osImages) {
// TODO Auto-generated constructor stub
result=osNameList;
context=mainActivity;
imageId=osImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView os_text;
ImageView os_img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.sample_gridlayout, null);
holder.os_text =(TextView) rowView.findViewById(R.id.os_texts);
holder.os_img =(ImageView) rowView.findViewById(R.id.os_images);
holder.os_text.setText(result[position]);
holder.os_img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
在您的活動中
CustomAdapter adapter = new CustomAdapter(this, String[] your_data, int[] img_arr);
gridView.setAdapter(adapter);
如前所述,您的列為3,行數為3。然后位置0, 1, 2
表示第一行, 3, 4, 5
表示第二行6, 7, 8
表示第三行。
因為您的網格是固定的,所以位置永遠不會改變,因此您基本上具有索引的項目:
0 1 2
3 4 5
6 7 8
您可以將數據映射到具有其索引的2D數組:
(0,0)(0,1)(0,2)
(1,0)(1,1)(1,2)
(2,0)(2,1)(2,2)
這樣,您可以檢查整個行和列。 對於第一個對角線,您可以檢查兩個索引等於(0,0),(1,1),(2,2)的條件。 對於第二個對角線,您可以檢查索引的總和為2(2,0),(1、1),(0、2)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.