在android中使用2个gridviews

时间:2017-06-17 07:30:55

标签: android gridview

在这里输入代码我使用2 gridview一个gridview和9X9单元格,一个有3x3单元格。 我已将9x9网格设置为imageview s (总共有81个图像视图)并且3x3有9个图像视图。

当我点击9x9网格时,它会在各自的位置显示图像。 现在我无法弄清楚如何在9x9网格中满足条件时在3x3网格中显示图像。它基本上是一个正在玩的tic tac toe游戏现在9个游戏正在两个人之间一次玩,当一组赢了我想要在游戏中显示该组的赢家

what i want

我附上了我想要的照片。 3x3网格视图与9x9网格视图重叠。

我的代码在

之下

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.auk01.tictactoegrand.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.auk01.tictactoegrand.MainActivity"
tools:showIn="@layout/activity_main">

<GridLayout
android:id="@+id/GrandGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="3"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<ImageView
    android:id="@+id/imageViewSet0"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet1"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet2"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet3"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet4"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet5"
    android:layout_width="116dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet6"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet7"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

<ImageView
    android:id="@+id/imageViewSet8"
    android:layout_width="100dp"
    android:layout_height="140dp"
    android:layout_margin="5dp" />

</GridLayout>

<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:background="@drawable/board9"
android:columnCount="9"
android:rowCount="9"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">


<ImageView
    android:id="@+id/imageView0"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="0"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="1"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="2"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="3"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="4"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="5"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView6"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="6"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView7"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="7"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView8"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="8"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView9"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="9"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView10"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="10"
    app:srcCompat="@drawable/invisable" />

//i have removed id/imageView11 to imageView75 just to explain my code in shoter
<ImageView
    android:id="@+id/imageView76"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="76"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView77"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="77"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView78"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="78"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView79"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="79"
    app:srcCompat="@drawable/invisable" />

<ImageView
    android:id="@+id/imageView80"
    android:layout_width="35dp"
    android:layout_height="46dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="2dp"
    android:onClick="gamelogic"
    android:tag="80"
    app:srcCompat="@drawable/invisable" />

</GridLayout>

<LinearLayout
android:id="@+id/WinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
android:padding="35dp">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="PlayAgain"
    android:text="Play Again" />
</LinearLayout>

</RelativeLayout>

mainactivity.java

package com.example.auk01.tictactoegrand;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

int ActivePlayer = 0; // 0 for red
int[] gameState = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};//2 means unplayed.
int[][] WinningLocation0 = {{0,1,2}, {9,10,11}, {18,19,20}, {0,9,18}, {1,10,19}, {2,11,20}, {0,10,20}, {2,10,18}};
int[][] WinningLocation1 = {{3,4,5}, {12,13,14}, {21,22,23}, {3,12,21}, {4,13,22}, {5,14,23}, {3,13,23}, {5,13,21}};
int[][] WinningLocation2 = {{6,7,8}, {15,16,17}, {24,25,26}, {6,15,24}, {7,16,25}, {8,17,26}, {6,16,26}, {8,16,24}};
int[][] WinningLocation3 = {{27,28,29}, {36,37,38}, {45,46,47}, {27,36,45}, {28,37,46}, {29,38,47}, {27,37,47}, {29,37,45}};
int[][] WinningLocation4 = {{30,31,32}, {39,40,41}, {48,49,50}, {30,39,48}, {31,40,49}, {32,41,50}, {30,40,50}, {32,40,48}};
int[][] WinningLocation5 = {{33,34,35}, {42,43,44}, {51,52,53}, {33,42,51}, {34,43,52}, {35,44,53}, {33,43,53}, {35,43,51}};
int[][] WinningLocation6 = {{54,55,56}, {63,64,65}, {72,73,74}, {54,63,72}, {55,64,73}, {56,65,74}, {54,64,74}, {56,64,72}};
int[][] WinningLocation7 = {{57,58,59}, {66,67,68}, {75,76,77}, {57,66,75}, {58,67,76}, {59,68,77}, {57,67,77}, {59,67,75}};
int[][] WinningLocation8 = {{60,61,62}, {69,70,71}, {78,79,80}, {60,69,78}, {61,70,79}, {62,71,80}, {60,70,80}, {62,70,78}};
boolean GameOver = false;

public void gamelogic(View view) {
    ImageView tappedview = (ImageView) view;

    int tappedLocation = Integer.parseInt(view.getTag().toString());

    if(gameState[tappedLocation] == 2 && !GameOver) {
        gameState[tappedLocation] = ActivePlayer;
        tappedview.setTranslationY(-3000f);
        if (ActivePlayer == 0) {
            tappedview.setImageResource(R.drawable.red);
            ActivePlayer = 1;
        } else if (ActivePlayer == 1) {
            tappedview.setImageResource(R.drawable.yellow);
            ActivePlayer = 0;
        }
        tappedview.animate().translationYBy(3000f).setDuration(10);


    }
    String msg = "";
    for (int[]WinningPosition : WinningLocation0){
        if(gameState[WinningPosition[0]] == gameState[WinningPosition[1]]
                && gameState[WinningPosition[1]] == gameState[WinningPosition[2]]
                && gameState[WinningPosition[0]] !=2){

            if(ActivePlayer == 0)
                Toast.makeText(getApplicationContext(), "0 IS THE WINNER Of Set Zero", Toast.LENGTH_LONG).show();
                GridLayout GrandGridLayout = (GridLayout) findViewById(R.id.GrandGridLayout);
                msg = "0 IS THE WINNER";
            if(ActivePlayer == 1)
                Toast.makeText(getApplicationContext(), "X IS THE WINNER Of Set Zero", Toast.LENGTH_LONG).show();
                msg = "X IS THE WINNER";
        }
    }

    for (int[]WinningPosition : WinningLocation1){
        if(gameState[WinningPosition[0]] == gameState[WinningPosition[1]]
                && gameState[WinningPosition[1]] == gameState[WinningPosition[2]]
                && gameState[WinningPosition[0]] !=2){

            if(ActivePlayer == 0)
                msg = "0 IS THE WINNER";
            if(ActivePlayer == 1)
                msg = "X IS THE WINNER";

            LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout);
            WinnerLayout.setVisibility(View.VISIBLE);

            TextView WinnerMsg = (TextView) findViewById(R.id.textView);
            WinnerMsg.setText(msg);
            GameOver = true;
        }
    }

}

public void PlayAgain(View view){
    LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout);
    WinnerLayout.setVisibility(View.INVISIBLE);
    GameOver = false;
    ActivePlayer = 0;

    for(int i = 0; i < gameState.length; i++)
        gameState[i] = 2;
    GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
    for(int i = 0; i < gridLayout.getChildCount(); i++)
        ((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    LinearLayout WinnerLayout = (LinearLayout) findViewById(R.id.WinnerLayout) ;
    WinnerLayout.setVisibility(View.INVISIBLE);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是所有代码现在我无法弄清楚如何在小网格中获取drawable \ red在我的情况下id.grandgridlayout(3x3) id.gridlayout是9x9

1 个答案:

答案 0 :(得分:0)

我们不讨论程序本身的逻辑或使用的“正确”模式。 我刚刚创建了一个简单的应用程序,以防万一在网格视图上单击一次预先加载了一些数据时左上角的三个X.点击后你会看到新的X出现在大网格下面的小网格上。

activity_main.xml中

<div id="container">
  <div class="new_menuSection">
    <div>
      <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </div>
  <section class="menuSection">
    <div>
      <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </section>
  <section class="menuSection">
    <div>
      <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </section>
  <section class="menuSection">
    <div>
      <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </section>

  <section class="menuSection">
    <div>
      <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </section>
</div>

AMain.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0"
tools:context="com.ad3luc.problemsolving.AMain">

    <GridView
        android:id="@+id/gridView_big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:numColumns="9"
        android:stretchMode="columnWidth" />

    <ImageView
        android:id="@+id/separator"
        android:layout_below="@id/gridView_big"
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:background="@color/colorPrimaryDark"/>

    <GridView
        android:id="@+id/gridView_small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/separator"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
</RelativeLayout>

我认为实现的解决方案是读取在大网格上进行的当前点击并匹配列表,地图,...存储所有点击的位置。条件匹配时,您需要更新列表,地图,...您将传递给适配器,然后您将传递给小网格视图。