GridLayoutActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import java.util.ArrayList; import java.util.Arrays; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; //This application uses some deprecated methods. //See UIViewPager for a more modern version of this application public class GridLayoutActivity extends Activity { protected static final String EXTRA_RES_ID = "POS"; private ArrayList<Integer> mThumbIdsFlowers = new ArrayList<Integer>( Arrays.asList(R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12)); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); // Create a new ImageAdapter and set it as the Adapter for this GridView gridview.setAdapter(new ImageAdapter(this, mThumbIdsFlowers)); // Set an setOnItemClickListener on the GridView gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Create an Intent to start the ImageViewActivity Intent intent = new Intent(GridLayoutActivity.this, ImageViewActivity.class); // Add the ID of the thumbnail to display as an Intent Extra intent.putExtra(EXTRA_RES_ID, (int) id); // Start the ImageViewActivity startActivity(intent); } }); } } |
ImageAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import java.util.List; import android.content.Context; 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 static final int PADDING = 8; private static final int WIDTH = 250; private static final int HEIGHT = 250; private Context mContext; private List<Integer> mThumbIds; // Store the list of image IDs public ImageAdapter(Context c, List<Integer> ids) { mContext = c; this.mThumbIds = ids; } // Return the number of items in the Adapter @Override public int getCount() { return mThumbIds.size(); } // Return the data item at position @Override public Object getItem(int position) { return mThumbIds.get(position); } // Will get called to provide the ID that // is passed to OnItemClickListener.onItemClick() @Override public long getItemId(int position) { return mThumbIds.get(position); } // Return an ImageView for each item referenced by the Adapter @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = (ImageView) convertView; // if convertView's not recycled, initialize some attributes if (imageView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT)); imageView.setPadding(PADDING, PADDING, PADDING, PADDING); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } imageView.setImageResource(mThumbIds.get(position)); return imageView; } } |
ImageViewActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ImageView; public class ImageViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the Intent used to start this Activity Intent intent = getIntent(); // Make a new ImageView ImageView imageView = new ImageView(getApplicationContext()); // Get the ID of the image to display and set it as the image for this ImageView imageView.setImageResource(intent.getIntExtra(GridLayoutActivity.EXTRA_RES_ID, 0)); setContentView(imageView); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /> </LinearLayout> |