Basic tutorial for Android -- 2.4.9 basic usage of the GridView (grid view)

Source: Internet
Author: User

Basic tutorial for Android -- 2.4.9 basic usage of the GridView (grid view)
1. related attributes:

The following are some properties in the GridView:

Android: columnWidth: Set the column width Android: gravity: Component Method Android: horizontalSpacing: Horizontal distance of each cell Android: verticalSpacing: Vertical distance of each cell Android: numColumns: Set the number of Columns Android: stretchMode: Set the stretch mode. The optional values are as follows:
None: Do not stretch; SpacingWidth: Gap Between stretched Elements
ColumnWidth: Only stretch the table element itself
SpacingWidthUniform: Both pull element spacing and stretch the gap between them air strikes
2. Example:

The following is a simple example to familiarize yourself with the use of this control:
(The Adapter used here is a reusable BaseAdapter that we can use in 2.4.7 to teach you how to write ~)

Implemented:

Code Implementation:

The first is the layout of the items in the GridView:Item_grid_icon.xml:


  
      
       
    
   
  

Then we write an entity object class:Icon. java:

/** * Created by Jay on 2015/9/24 0024. */public class Icon {    private int iId;    private String iName;    public Icon() {    }    public Icon(int iId, String iName) {        this.iId = iId;        this.iName = iName;    }    public int getiId() {        return iId;    }    public String getiName() {        return iName;    }    public void setiId(int iId) {        this.iId = iId;    }    public void setiName(String iName) {        this.iName = iName;    }}

The layout of MainActivity and Java code

Activity_main.xml:


      
       
   
  

MainActivity. java:

Public class MainActivity extends AppCompatActivity {private Context mContext; private GridView grid_photo; private BaseAdapter mAdapter = null; private ArrayList
  
   
MData = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mContext = MainActivity. this; grid_photo = (GridView) findViewById (R. id. grid_photo); mData = new ArrayList
   
    
(); MData. add (new Icon (R. mipmap. iv_icon_1, icon 1); mData. add (new Icon (R. mipmap. iv_icon_2, Icon 2); mData. add (new Icon (R. mipmap. iv_icon_3, icon 3); mData. add (new Icon (R. mipmap. iv_icon_4, icon 4); mData. add (new Icon (R. mipmap. iv_icon_5, icon 5); mData. add (new Icon (R. mipmap. iv_icon_6, icon 6); mData. add (new Icon (R. mipmap. iv_icon_7, icon 7); mAdapter = new MyAdapter
    
     
(MData, R. layout. item_grid_icon) {@ Override public void bindView (ViewHolder holder, Icon obj) {holder. setImageResource (R. id. img_icon, obj. getiId (); holder.setText(R.id.txt _ icon, obj. getiName () ;}}; grid_photo.setAdapter (mAdapter); grid_photo.setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView
     Parent, View view, int position, long id) {Toast. makeText (mContext, you have clicked ~ + Position ++ ~ Item, Toast. LENGTH_SHORT). show ();}});}}
    
   
  

Well, the code is very simple ~

  Summary:

This section introduces the second UI control to use Adapter-grid view GridView, which is easy to use ~
You can scale it according to your needs, for example, using the GridView to display mobile album ~ Well, that's all,
Thank you ~

 

Related Article

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.