首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在android中使用2个网格视图

在android中使用2个网格视图
EN

Stack Overflow用户
提问于 2017-06-17 07:30:55
回答 1查看 88关注 0票数 0

输入下面的代码--我使用的是2个网格视图,一个带有9X9单元格的网格视图,以及一个带有3x3单元格的网格视图。我用imageviews设置了9x9网格(总共有81个图像视图),3x3有9个图像视图。

当我在9x9网格中单击时,它会在各自的位置显示图像。现在,当9x9网格中满足条件时,我想不出如何在3x3网格中显示图像。这基本上是一种抽搐脚趾游戏,现在有9场比赛是在两个人之间同时进行的,当一盘比赛获胜时,我想在比赛中向获胜者展示。

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

我的密码在下面

activity_main.xml

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
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);
}
}

这就是我现在无法理解的所有代码,如何在小网格视图中获得可绘制的红色,在我的例子中,id.grandgridlayout (3x3) id.gridlayout是9x9。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-17 09:23:55

我们不讨论程序本身的逻辑或使用的“正确”模式。我刚刚创建了一个简单的应用程序,如果您单击网格视图中的某个数据预加载时,左上角的3X会被选中。单击后,您将看到新的X出现在大网格下面的小网格上。

activity_main.xml

代码语言:javascript
运行
复制
<?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>

AMain.java

代码语言:javascript
运行
复制
public class AMain extends AppCompatActivity {

    private GridView gGridViewBig;
    private GridView gGridViewSmall;

    static final String[] ticTacToeBig = new String[] {
            "X", "O", "", "", "", "", "", "", "",
            "X", "", "O", "", "", "", "", "", "",
            "X", "O", "", "", "", "", "", "", "",
            "", "", "", "", "", "X", "", "", "",
            "", "", "", "", "", "", "O", "", "",
            "", "", "X", "", "", "", "", "", "",
            "", "", "O", "", "", "", "O", "X", "X",
            "", "", "", "", "", "", "", "O", "",
            "", "", "", "", "", "", "X", "", "O"
    };

    static final String[] ticTacToeSmall = new String[] {
            "", "", "",
            "", "", "",
            "", "", "",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gGridViewBig = (GridView) findViewById(R.id.gridView_big);
        gGridViewSmall = (GridView) findViewById(R.id.gridView_small);

        ArrayAdapter<String> adapterBig = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, ticTacToeBig);

        final ArrayAdapter<String> adapterSmall = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, ticTacToeSmall);

        gGridViewBig.setAdapter(adapterBig);
        gGridViewSmall.setAdapter(adapterSmall);

        gGridViewBig.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                if(ticTacToeBig[0].equals("X") && ticTacToeBig[9].equals("X") && ticTacToeBig[18].equals("X")) {

                    ticTacToeSmall[0] = "X";
                    gGridViewSmall.setAdapter(adapterSmall);
                }
            }
        });
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44601968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档