Android learning-GridView, androidgridview
Background
GridView is frequently used in Android development like ListView. For example, we often use Quick graph browsing, which has the option to change the layout of the image to a grid (that is, a GridView. There is also the X-artifact-Momo's search world also uses the GridView.
Definition
GridViewIsViewGroupThat displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout usingListAdapter
A grid view is a set of views that display grid items in a two-dimensional rolling grid. Grid items are automatically inserted into the layout using ListAdapter.
Demo
1. Use SimpleAdapter for adaptation
MainActivity
1 package com. johntsai. gridviewdemo; 2 3 import java. util. arrayList; 4 import java. util. hashMap; 5 6 import android. app. activity; 7 import android. content. intent; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. widget. adapterView; 11 import android. widget. adapterView. onItemClickListener; 12 import android. widget. gridView; 13 import android. widget. simpleAdapter; 14 import androi D. widget. toast; 15 16 public class MainActivity extends Activity {17 private int ids [] = {18 R. drawable. aaa, 19 R. drawable. bbb, 20 R. drawable. ccc, 21 R. drawable. ddd, 22 R. drawable. eee, 23 R. drawable. fff, 24 R. drawable. ggg, 25 R. drawable. hhh, 26 R. drawable. aaa27}; 28 private GridView; 29 @ Override30 protected void onCreate (Bundle savedInstanceState) {31 super. onCreate (savedInstanceState); 32 setContent View (R. layout. activity_main); 33 34 gridView = (GridView) findViewById (R. id. gridview); 35 36 ArrayList <HashMap <String, Object> images = new ArrayList <HashMap <String, Object> (); 37 for (int I = 0; I <ids. length; I ++) {38 HashMap <String, Object> map = new HashMap <String, Object> (); 39 map. put ("Image", ids [I]); 40 map. put ("Id", "Android" + I + "); 41 images. add (map); 42} 43 44 SimpleAdapter adapter = new SimpleAdapte R (this, 45 images, R. layout. gridview_item, 46 new String [] {"Image", "Id"}, new int [] {R. id. imageItem, R. id. idItem}); 47 gridView. setAdapter (adapter); 48 49 gridView. setOnItemClickListener (new OnItemClickListener () {50 51 @ Override52 public void onItemClick (AdapterView <?> Parent, View view, 53 int position, long id) {54 Toast. makeText (getApplicationContext (), 55 "you clicked" + position + ", Toast. LENGTH_LONG ). show (); 56 57} 58}); 59} 60}
Main Interface layout File
<? Xml version = "1.0" encoding = "UTF-8"?> <GridView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/gridview" android: layout_width = "match_parent" android: layout_height = "match_parent" android: columnWidth = "90dp" android: numColumns = "auto_fit" android: stretchMode = "columnWidth" android: gravity = "center"/> <! -- Set the column width of android: columnWidth = "90dp" to 90dp android: numColumns = "auto_fit" to auto android: stretchMode = "columnWidth" scaling and column width synchronization -->
Layout file of the GridView item
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="4dip"> 6 7 <ImageView 8 android:id="@+id/imageItem" 9 android:layout_height="70dp"10 android:layout_width="70dp"11 android:layout_centerHorizontal="true"12 />13 <TextView 14 android:id="@+id/idItem"15 android:layout_height="wrap_content"16 android:layout_width="70dp"17 android:layout_centerHorizontal="true"18 android:layout_below="@id/imageItem"19 />20 </RelativeLayout>
Running effect:
2. Use the ImageAdapter that inherits from BaseAdapter for adaptation
1 package com. johntsai. gridviewdemo; 2 3 import android. content. context; 4 import android. view. view; 5 import android. view. viewGroup; 6 import android. widget. baseAdapter; 7 import android. widget. gridView; 8 import android. widget. imageView; 9 10 public class ImageAdapter extends BaseAdapter {11 public Integer [] ImagesIds = {12 R. drawable. aaa, 13 R. drawable. bbb, 14 R. drawable. ccc, 15 R. drawable. ddd, 16 R. drawable. eee, 17 R. drawable. fff, 18 R. drawable. ggg, 19 R. drawable. hhh20}; 21 private Context context; 22 // constructor 23 public ImageAdapter (Context c) {24 context = c; 25} 26 @ Override27 public int getCount () {28 return ImagesIds. length; 29} 30 31 @ Override32 public Object getItem (int position) {33 return null; 34} 35 36 @ Override37 public long getItemId (int position) {38 return 0; 39} 40 41 @ Override42 public View getView (int position, View convertView, ViewGroup parent) {43 ImageView imageView; 44 if (convertView = null) {45 imageView = new ImageView (context); 46 imageView. setLayoutParams (new GridView. layoutParams (100,100); 47 imageView. setScaleType (ImageView. scaleType. CENTER_CROP); 48 imageView. setPadding (0, 0, 0, 0); 49} else {50 imageView = (ImageView) convertView; 51} 52 53 imageView. setImageResource (ImagesIds [position]); 54 return imageView; 55} 56 57}
MainActivity
1 public class MainActivity extends Activity {2 private GridView; 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. activity_main); 7 8 gridView = (GridView) findViewById (R. id. gridview); 9 gridView. setAdapter (new ImageAdapter (getApplicationContext (); 10 11 gridView. setOnItemClickListener (new OnItemClickListen Er () {12 13 @ Override14 public void onItemClick (AdapterView <?> Parent, View view, 15 int position, long id) {16 Toast. makeText (getApplicationContext (), 17 "you clicked" + position + ", Toast. LENGTH_LONG ). show (); 18 19} 20}); 21} 22}