The use of 35:gridview in Android development

Source: Internet
Author: User

In the process of using ASP to develop the software, I also found that there is a control in Android, which is a bit similar, using the adapter to bind the data to this control, and then show the data, it should say it is a very common control like the ListView, so we should learn Learning how to use it, the theoretical knowledge is not much, is a display data control, the inheritance relationship is as follows:
public class GridView
Extends Abslistview
Java.lang.Object
Android.view.View
Android.view.ViewGroup
Android.widget.adapterview<t extends Android.widget.adapter>
Android.widget.AbsListView
Android.widget.GridView
Start with our example:
For this example, we want to achieve two kinds of effects:
The first shows a list of pictures of puppies, click the picture and trigger a message
The second display of the margin of Liangshan heroes Avatar, name, age information, click on a row will display the name in the title
First step, design page
Res\layout\gridview.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" >

<gridview android:id= "@+id/mygridview" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" android:columnwidth= "90DP"
android:numcolumns= "Auto_fit" android:horizontalspacing= "10DP"
android:verticalspacing= "10DP" android:stretchmode= "ColumnWidth"
android:gravity= "Center" >
</GridView>
</RelativeLayout>

Res\layout\personview.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<imageview android:id= "@+id/imghead" android:layout_width= "wrap_content" android:src= "@drawable/icon" Android: layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_alignparentleft= "true" ></ Imageview>
<textview android:id= "@+id/tvname" android:text= "name" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignbottom= "@+id/imghead" android:layout_torightof= "@+id/imgHead" Android:layout_ marginleft= "40DP" android:layout_marginbottom= "14DP" ></TextView>
<textview android:id= "@+id/tvage" android:text= "Age" android:layout_width= "wrap_content" android:layout_height= " Wrap_content "android:layout_aligntop=" @+id/tvname "android:layout_torightof=" @+id/tvname "Android:layout_ marginleft= "68DP" ></TextView>
</RelativeLayout>


Step two, design activity Gridviewactivity.java


/**
* Usage of the GridView
*/
Package Com.figo.helloworld;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.GridView;
Import Android.widget.ImageView;
Import Android.widget.SimpleAdapter;
Import Android.widget.Toast;

/**
* @author Zhuzhifei
*
*/
public class Gridviewactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.gridview);
Assembling data for GV
GridView GridView = (GridView) Findviewbyid (R.id.mygridview);
Using the system View ImageView
Gridview.setadapter (New Imageadapter (this));
Gridview.setcolumnwidth (90);
Add a click-line event
Gridview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview parent, view view,
int position, long ID) {
Toast.maketext (Gridviewactivity.this, "dog" +id+ "Wang Woo",
Toast.length_short). Show ();
//
//     }
//  });
Display picture + text with a simple adapter
Simpleadapter adapter = Getsimpleadapter ();
Gridview.setadapter (adapter);
Gridview.setcolumnwidth (200);
Add a click-line event
Gridview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview parent, view view, int position, long ID) {
In this case, ARG2=ARG3
map<string, object> item = (map<string, object>) parent.getitematposition (position);
Displays the Itemtext of the selected item
Settitle (Item.get ("name"). toString ());
}
});


}

/**
* Simple Adapter Display picture + text
*
* @return
*/
Private Simpleadapter Getsimpleadapter () {
Generates a dynamic array and transfers data
list<map<string, object>> list = new arraylist<map<string, object>> ();
map<string, object> map = new hashmap<string, object> ();
Map.put ("Name", "Song Jiang");
Map.put ("Age", "33 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Lu Junyi");
Map.put ("Age", "32 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Guan Sheng");
Map.put ("Age", "28 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Lin");
Map.put ("Age", "30 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map.put ("name", "Wu use");
Map.put ("Age", "33 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Wu Song");
Map.put ("Age", "32 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Gongsun wins");
Map.put ("Age", "28 years old");
Map.put ("img", R.drawable.icon);
List.add (map);
Map = new hashmap<string, object> ();
Map.put ("name", "Farong");
Map.put ("Age", "30 years old");
Map.put ("img", R.drawable.icon);
List.add (map);

Build Adapter
Simpleadapter adapter = new Simpleadapter (this, list,
R.layout.personview, new string[] {"img",
"Name", "Age"}, new int[] {r.id.imghead,
R.id.tvname,r.id.tvage});

return adapter;
}
Picture Adapter
Private class Imageadapter extends Baseadapter {
Private Context Mcontext;
Private Layoutinflater Minflater;
Private list<map<string, object>> mdata;
Dog Picture ID Collection
Private integer[] Mdogs = {R.drawable.doga, R.DRAWABLE.DOGB,
R.DRAWABLE.DOGC, R.drawable.dogz, R.drawable.dogf, R.drawable.dogr,
R.DRAWABLE.DOGL, R.DRAWABLE.DOGV, R.drawable.dogu,
R.DRAWABLE.DOGJ,R.DRAWABLE.DOGK,R.DRAWABLE.DOGM,
R.drawable.dogo,r.drawable.dogs,r.drawable.dogw
};
Public Imageadapter (Context context) {
This.mcontext = context;
}

@Override
public int GetCount () {
return mdogs.length;
}

@Override
Public Object getItem (int position) {

return mdogs[position];
}

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

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {

Use the picture actually here also can use TextView
ImageView ImageView;
if (Convertview = = null) {
ImageView = new ImageView (mcontext);
Imageview.setlayoutparams (New Gridview.layoutparams (85, 85));
Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);
Imageview.setpadding (8, 8, 8, 8);
} else {
ImageView = (ImageView) Convertview;
}
Imageview.setimageresource (Mdogs[position]);
return imageView;


}
}


}
Fourth step, Operation effect

The use of 35:gridview in Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.