Dynamic display of Android development learning Note-gridview

Source: Internet
Author: User

1. Add GridItem Layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"android:orientation= "vertical" >    <ImageViewAndroid:id= "@+id/iv_image"Android:layout_width= "Wrap_content"Android:layout_height= "60DP"android:src= "@drawable/app" />    <TextViewAndroid:id= "@+id/tv_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Set Center" /></LinearLayout>
2. Add GridView to main interface
< GridView          Android:id = "@+id/grid_main"          android:layout_width= "Wrap_content"        android:layout_height= "Wrap_ Content "        android:numcolumns=" 3 "        >            </ GridView >
3. Add a custom adapter class and set values for the controls in the custom GridItem layout file
protected classMygridadapterextendsBaseadapter {@Override Public intGetCount () {//TODO auto-generated Method Stub            returntitles.length; } @Override PublicObject GetItem (intposition) {            //TODO auto-generated Method Stub            return NULL; } @Override Public LongGetitemid (intposition) {            //TODO auto-generated Method Stub            return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {            //TODO auto-generated Method StubView view = View.inflate (mainactivity. This, R.layout.list_item_main,NULL); TextView title=(TextView) View.findviewbyid (r.id.tv_title);                        Title.settext (Titles[position]); returnview; }
4. Set the GridItem in the main interface
Private GridView Grid_main; Private Mygridadapter Adapter; Private Static String []titles = {"Set center 1", "Set Center 2", "Set Center 3", "Set Center 4", "Set Center 5", "Set Center 6", "Set Center 7", "Set Center 8", "Set Center 9"=   New  mygridadapter (); Grid_main.setadapter (adapter);
5. Add Item Click event
Grid_main.setonitemclicklistener (new  Onitemclicklistener () {            @Override            public void Onitemclick (adapterview<?> Parent, view view,                    intlong  ID) {                 // TODO auto-generated Method Stub                 Consoleitemclick (position);            }        });
6. Display

Dynamic display of Android development learning Note-gridview

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.