Add a product to the GridView, and add a product to the GridView

Source: Internet
Author: User

Add a product to the GridView, and add a product to the GridView

Yes

Define a GridView and add a product on it.

First define the product Adapter

1 package org. xml. demo; 2 3 import ogg. huanxin. huadong. r; 4 import android. content. context; 5 import android. view. layoutInflater; 6 import android. view. view; 7 import android. view. viewGroup; 8 import android. widget. baseAdapter; 9 import android. widget. linearLayout; 10 import android. widget. textView; 11 12 public class ProducteGridAdapter extends BaseAdapter {13 private Context context; 14 15 public ProducteGridAdapter (Context context) {16 super (); 17 this. context = context; 18} 19 20 private int [] costs = {900,768,868,554,610,152,199,299,544,366}; 21 private String [] title = {"casual men's wear", "Women's Wear", "Children's wear ", "Mobile Phone", "casual men's wear", "Women's Wear", 22 "Children's wear", "Mobile Phone", "casual men's wear", "casual men's wear "}; 23 24 @ Override25 public int getCount () {26 // number of entries in the dataset represented in this adapter 27 return costs. length; 28} 29 30 @ Override31 public Object getItem (int arg0) {32 // (get the data item corresponding to the specified index) 33 return arg0; 34} 35 36 @ Override37 public long getItemId (int arg0) {38 // retrieve the row id39 return arg0 corresponding to the specified index in the list; 40} 41 42 @ Override43 public View getView (int position, View convertView, ViewGroup parent) {44 // obtain a view that specifies an index in the dataset to display data 45 Holder holder = null; 46 if (convertView = null) {47 holder = new Holder (); 48 // load the layout according to the custom Layout 49 LayoutInflater mInflater = LayoutInflater. from (context); 50 convertView = mInflater. inflate (R. layout. home_produce, null); 51 holder. ll_left = (LinearLayout) convertView52. findViewById (R. id. ll_left); 53 holder. ll_right = (LinearLayout) convertView54. findViewById (R. id. ll_right); 55 holder. product_cost = (TextView) convertView56. findViewById (R. id. product_cost); 57 holder. product_title = (TextView) convertView58. findViewById (R. id. product_title); 59 // Save the configured layout to the cache and set it in the Tag so that the Tag60 convertView can be conveniently retrieved later. setTag (holder); 61 62} else {63 64 holder = (Holder) convertView. getTag (); 65 66} 67 68 holder. product_cost.setText (costs [position] + ""); 69 holder. product_title.setText (title [position]); 70 71 return convertView; 72} 73 74 private static final class Holder {75 private TextView product_title; 76 TextView product_cost; 77 LinearLayout ll_left; 78 LinearLayout ll_right; 79} 80}View Code

R. layout. home_produce

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: id = "@ + id/product" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: background = "# eee" 7 android: gravity = "center_horizontal" 8 android: orientation = "vertical"> 9 10 <ImageView11 android: id = "@ + id/image_product" 12 android: layout_width = "match_parent" 13 android: layout_height = "180dp" 14 android: scaleType = "fitXY" 15 android: src = "@ drawable/name"/> 16 17 <TextView18 android: id = "@ + id/product_title" 19 android: layout_width = "match_parent" 20 android: layout_height = "wrap_content" 21 android: ellipsize = "end" 22 android: gravity = "center_horizontal" 23 android: maxLines = "1" 24 android: paddingBottom = "5dp" 25 android: paddingLeft = "10dp" 26 android: paddingRight = "10dp" 27 android: paddingTop = "5dp" 28 android: text = "@ string/product" 29 android: textSize = "15sp"/> 30 31 <LinearLayout32 android: id = "@ + id/product_ll" 33 android: layout_width = "match_parent" 34 android: layout_height = "30dp"> 35 36 <LinearLayout37 android: id = "@ + id/ll_left" 38 android: layout_width = "0dp" 39 android: layout_height = "match_parent" 40 android: layout_weight = "1" 41 android: paddingLeft = "15dp"> 42 43 <TextView44 android: layout_width = "wrap_content" 45 android: layout_height = "match_parent" 46 android: gravity = "center_vertical" 47 android: text = "@ string/money" 48 android: textColor = "@ android: color/holo_blue_light" 49 android: textSize = "16sp"/> 50 51 <TextView52 android: layout_width = "wrap_content" 53 android: layout_height = "match_parent" 54 android: layout_marginLeft = "5dp" 55 android: id = "@ + id/product_cost" 56 android: gravity = "center_vertical" 57 android: text = "@ string/price" 58 android: textSize = "16sp"> 59 </TextView> 60 </LinearLayout> 61 62 <LinearLayout63 android: id = "@ + id/ll_right" 64 android: layout_width = "0dp" 65 android: layout_height = "match_parent" 66 android: layout_weight = "1" 67 android: gravity = "right" 68 android: paddingRight = "15dp"> 69 70 <TextView71 android: layout_width = "wrap_content" 72 android: layout_height = "match_parent" 73 android: gravity = "center_vertical" 74 android: text = "@ string/xiaoshou" 75 android: textColor = "@ android: color/holo_blue_light" 76 android: textSize = "16sp"/> 77 78 <TextView79 android: layout_width = "wrap_content" 80 android: layout_height = "match_parent" 81 android: layout_marginLeft = "5dp" 82 android: gravity = "center_vertical" 83 android: text = "@ string/much" 84 android: textSize = "16sp"> 85 </TextView> 86 </LinearLayout> 87 </LinearLayout> 88 89 </LinearLayout>View Code

Main Code

Package org. xml. demo; import ogg. huanxin. huadong. r; import android. app. activity; import android. OS. bundle; public class MyProducte extends Activity {// private GridView productGridView; private MyGridView product_gridView; private ProducteGridAdapter producteGridAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // defines the layout variable super. setContentView (R. layout. home_product); // obtain the control product_gridView = (MyGridView) super. findViewById (R. id. product); // set the adapter producteGridAdapter = new ProducteGridAdapter (this); product_gridView.setAdapter (producteGridAdapter );}}

Layout xml

First define a MyGridView

1 package org. xml. demo; 2 3 import android. content. context; 4 import android. util. attributeSet; 5 import android. widget. gridView; 6 7 public class MyGridView extends GridView {8 9 public MyGridView (Context context, AttributeSet attrs) {10 super (context, attrs); 11} 12 13 public MyGridView (Context context) {14 super (context); 15} 16 17 public MyGridView (Context context, AttributeSet attrs, int defStyle) {18 super (context, attrs, defStyle ); 19} 20 21 @ Override22 public void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {23 24 int expandSpec = MeasureSpec. makeMeasureSpec (Integer. MAX_VALUE> 2, 25 MeasureSpec. AT_MOST); 26 super. onMeasure (widthMeasureSpec, expandSpec); 27} 28 29}View Code
<? 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: background = "# eee" android: orientation = "vertical"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "15dp" android: gravity = "center_vertical" android: paddingBottom = "4dp" android: paddingTop = "5dp" android: text = "all products" android: textSize = "15sp"/> <View android: layout_width = "fill_parent" android: layout_height = "1dp" android: background = "@ android: color/white"/> <ScrollView android: layout_width = "match_parent" android: layout_height = "wrap_content"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <org. xml. demo. myGridView android: id = "@ + id/product" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: horizontalSpacing = "5dp" android: numColumns = "2" android: paddingBottom = "10dp" android: paddingLeft = "7dp" android: paddingRight = "7dp" android: paddingTop = "5dp" android: verticalSpacing = "8dp"/> </LinearLayout> </ScrollView> </LinearLayout>

 

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.