0

我正在尝试为 Android 制作一个简单的配对记忆游戏。

我试图将两个网格视图相互叠加,顶部网格视图“覆盖”底部网格视图的内容。所以顶部的gridview遮住了下面的图标,当按下顶部gridview中的一个tile时,它会暴露下面的图标。

然而,目前只有底部的网格视图显示(grid_view_bottom),我不知道为什么。即使我注释掉底部的 gridview 代码,顶部的 gridview 也不会出现。

任何帮助世界都会受到赞赏。

在 onCreate 方法中,我有:

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


    gridViewTop = (GridView) findViewById(R.id.grid_view_top);
    gridViewBottom = (GridView) findViewById(R.id.grid_view_bottom);
    gridViewTop.setNumColumns(mColumns);
    gridViewBottom.setNumColumns(mColumns);
    adapterBottom = new ImageAdapter(this, mColumns, mRows);
    adapterTop = new TopImageAdapter(this, mColumns, mRows);

    gridViewTop.setAdapter(adapterTop);
    gridViewBottom.setAdapter(adapterBottom);   
    gridViewTop.setOnItemClickListener(this);
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="2dp"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:paddingTop="2dp"
    tools:context=".MainActivity">

<GridView
    android:id="@+id/grid_view_top"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/info"

    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:listSelector="#00000000"
    android:numColumns="6"
    android:verticalSpacing="1dp" ></GridView>

 <GridView
    android:id="@+id/grid_view_bottom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/info"
    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:listSelector="#00000000"
    android:numColumns="6"
    android:verticalSpacing="1dp"></GridView>

 <TextView
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dimens_32"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="bottom"
    android:text="@string/text"
    android:textSize="22sp"></TextView>
 </RelativeLayout>

TopImageAdapter.java

public class TopImageAdapter extends BaseAdapter {

private Context mContext;
private int mColumns;
private int mRows;
private int mFrontImage = R.drawable.front;

public TopImageAdapter(Context context, int cols, int rows) {
    mContext = context;
    mColumns = cols;
    mRows = rows;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = (ImageView) convertView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setImageDrawable(mContext.getResources().getDrawable(mFrontImage));
        int width = mContext.getResources().getDisplayMetrics().widthPixels - 10;
        int height = mContext.getResources().getDisplayMetrics().heightPixels - 170;
        int scaleX = width / mColumns;
        int scaleY = height / mRows;
        imageView.setLayoutParams(new GridView.LayoutParams(scaleX, scaleY));
        imageView.setScaleType(ScaleType.CENTER_INSIDE);
    }

    return imageView;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

}

底部图像适配器 (ImageAdapter.java)

public class ImageAdapter extends BaseAdapter {

private Context mContext;
private int mColumns;
private int mRows;
private int mTotalViews;
private Integer[] mThumbIds = { array of images };
private ArrayList<Integer> collection = new ArrayList<Integer>();
private boolean[] enabledViews;

public ImageAdapter(Context c, int cols, int rows) {
    mContext = c;
    mColumns = cols;
    mRows = rows;
    mTotalViews = cols * rows;
    enabledViews = new boolean[mTotalViews];
    Arrays.fill(enabledViews, Boolean.TRUE);
    populateList();
}

private void populateList() {
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < mTotalViews / 2; j++) {
            collection.add(mThumbIds[j]);
        }
    }
    Collections.shuffle(collection);
}

public void setEnabled(boolean flag, int position) {
    enabledViews[position] = flag;
}

public int getItemResourceId(int position) {
    return collection.get(position);
}

@Override
public int getCount() {
    return collection.size();
}

@Override
public Object getItem(int position) {
    return collection.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = (ImageView) convertView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setImageDrawable(mContext.getResources().getDrawable( collection.get(position)));
        int width = mContext.getResources().getDisplayMetrics().widthPixels - 10;
        int height = mContext.getResources().getDisplayMetrics().heightPixels - 170;
        int scaleX = width / mColumns;
        int scaleY = height / mRows;
        imageView
                .setLayoutParams(new GridView.LayoutParams(scaleX, scaleY));
        imageView.setScaleType(ScaleType.CENTER_INSIDE);
    }
    return imageView;
}
4

1 回答 1

0

在 TopImageAdapter

@Override
public int getCount() {
    return 0;
}

是您的问题,您需要返回在底部网格视图中显示的集合的大小。但我建议只使用一个 GridView 并在 onClickEvent 上更改显示的图像......使用 GridViews 并不是一个好主意!

于 2013-08-14T12:49:30.100 回答