Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note most of my code isn't original, but patchwork code done on the basis found here: http://developer.android.com/guide/topics/ui/layout/gridview.html and found here: http://coding4ataraxia.blogspot.com/2011/06/how-to-display-chessboard-on-android.html
- package com.bansal.hellogridview;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.Toast;
- public class HelloGridView extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- GridView gridview = (GridView) findViewById(R.id.gridview);
- gridview.setAdapter(new ImageAdapter(this));
- gridview.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
- Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- ---------------------------------------------------------------------------------------------------------
- package com.bansal.hellogridview;
- import android.content.Context;
- import android.app.Activity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- public class ImageAdapter extends BaseAdapter {
- private Context mContext;
- public ImageAdapter(Context c) {
- mContext = c;
- }
- public int getCount() {
- return mThumbIds.length;
- }
- public Object getItem(int position) {
- return null;
- }
- public long getItemId(int position) {
- return 0;
- }
- // create a new ImageView for each item referenced by the Adapter
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater layoutInflater = ((Activity)mContext).getLayoutInflater (); // we get a reference to the activity
- squareContainerView =
- layoutInflater.inflate(R.layout.square, null);
- // Background
- final ImageView squareView =
- (ImageView)squareContainerView.findViewById(R.id.square_background);
- squareView.setImageResource(this.mThumbIds[(position + position/8)%2]);
- if (position % 2 == 0) { //mock test
- // Add The piece
- final ImageView pieceView =
- (ImageView)squareContainerView.findViewById(R.id.piece);
- pieceView.setImageResource(R.drawable.ic_launcher);
- pieceView.setTag(position);
- }
- }
- else {
- squareContainerView = convertView;
- }
- return squareContainerView;
- }
- // references to our images
- private Integer[] mThumbIds = {
- // R.drawable.sample_2, R.drawable.sample_3,
- // R.drawable.sample_4, R.drawable.sample_5,
- // R.drawable.sample_6, R.drawable.sample_7,
- // R.drawable.sample_0, R.drawable.sample_1,
- // R.drawable.sample_2, R.drawable.sample_3,
- // R.drawable.sample_4, R.drawable.sample_5,
- // R.drawable.sample_6, R.drawable.sample_7,
- // R.drawable.sample_0, R.drawable.sample_1,
- // R.drawable.sample_2, R.drawable.sample_3,
- // R.drawable.sample_4, R.drawable.sample_5,
- // R.drawable.sample_6, R.drawable.sample_7
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
- };
- }
- -----------------------------------------------------------------
- (square.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/square"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- android:background="#00000000">
- <ImageView android:id="@+id/square_background"
- android:layout_width="40dp"
- android:layout_height="35dp"
- android:background="#ffff0000"
- />
- <ImageView android:id="@+id/piece"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </FrameLayout>
- ---------------------------------------------------------------------
- (main.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:numColumns="8"
- android:padding="1dp"
- android:verticalSpacing="2dp"
- android:horizontalSpacing="2dp"
- android:columnWidth="60dp"
- android:stretchMode="columnWidth"
- android:gravity="center_horizontal"
- />
- ----------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement