覆盖两个网格视图

时间:2013-08-14 12:44:03

标签: android android-layout

我正在尝试为Android制作简单的双记忆游戏。

我试图将两个网格视图相互叠加,顶部gridview'覆盖'底部gridview的内容。因此,顶部gridview会遮盖下面的图标,当按下顶部gridview中的图块时,它会显示下面的图标。

然而,目前只有底部gridview显示(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;
}

1 个答案:

答案 0 :(得分:0)

TopImageAdapter中的

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

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