Android UI Programming (1)--nine Gongge (GridView)

Source: Internet
Author: User

Reference Blog: http://blog.csdn.net/xyz_lmn/article/details/6906255

Summary :

1. The GridView (grid view) displays the content by column and column, and each grid can be populated with existing layouts or custom layouts, and the GridView can display multiple meshes per row, that is, the number of columns.

2. The GridView needs to be used in conjunction with an adapter (Adapter), using the Setadapter method in the instance object of the GridView class to initialize the grid view, which is Gridview.setadapter (Myadapter).

3, each grid in the GridView needs to be data packaged into a collection, the collection as a parameter to initialize the adapter (Adapter) Instance object, in the adapter (Adapter) GetView method to get the data in the collection, this data to initialize the corresponding grid control.

4. The GetView method in the adapter (Adapter) is called when setting up the adapter and updating the adapter data, i.e. Gridview.setadapter (Myadapter) and myadapter.notifydatasetchanged () is called when

5, in order to realize the isolation between the grid, the first need to set the layout of the grid to fill the background with the grid is the layout of the background color inconsistent, that is, the Mylayout.xml layout background and Activity_ The background color of the main.xml layout is inconsistent, and you also need to set the spacing between the grid and the grid so that the grid is detached.

5, to achieve the selected (click) of the grid to render a different color, you need to know the clicked Grid item number, and call Myadapter.notifydatasetchanged () to update the adapter data, Finally, the item number of the grid is judged in the GetView method in the adapter, and if equal sets the background color that you want to render (referring to the background color of the view).

6, Android implementation of full-screen display method, we all know that some of the features of Android implementation often have two methods: one is to set the corresponding attributes in the XML file, the other is implemented in code. The same Android can be implemented in full screen in the following two ways

(1), in the Androidmanifest.xml configuration file <activity> tag add attributes

Android:theme= "@android: Style/theme.notitlebar.fullscreen"
(2), add the following two statements between super () and Setcontentview () two methods in the activity's OnCreate () method

This.requestwindowfeature (window.feature_no_title);//Remove the title bar This.getwindow (). SetFlags ( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//Remove Information Bar
7, some special properties of the GridView

android:numcolumns= "Auto_fit" the number of columns in the//gridview is set to Automatic

android:columnwidth= "90DP" //width of each column, which is the width of item

android:stretchmode= "ColumnWidth" //Zoom synchronized with column width size

android:verticalspacing= "10DP" margin between//two rows

android:horizontalspacing= "10DP" margin between//two columns

android:cachecolorhint= "#00000000" //Remove the default black background when dragging

android:listselector= "#00000000" //Remove the yellow background when selected

android:scrollbars= "None" //Hide the GridView scroll bar

android:fadescrollbars= "true" //set to True to enable auto-hide and display of scroll bars

android:fastscrollenabled= "true" //gridview a fast scrolling button (at least 4 page scrolling is displayed)

android:fadingedge= "None" //gridview fading (Fade) edge color is empty, the default value is vertical. (Can be understood as the top and bottom edge of the hint color)

android:fadingedgelength= "10dip" //define the length of the fading (fade) edge

android:stackfrombottom= "true" //set to True, your list will appear at the bottom of your list

android:transcriptmode= "Alwaysscroll" //When you add data dynamically, the list will automatically scroll down the latest entries to automatically scroll to the viewable range

android:drawselectorontop= "false" //Click on a record and the color will be the background color after the record, the text of the content is visible (default is False)

androidmanifest.xml--did not make any changes to create the project default

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "    package=" Com.wxl.ninebox "    android:versioncode=" 1 "    android:versionname=" 1.0 ">    < USES-SDK        android:minsdkversion= "8"        android:targetsdkversion= "/>    <application        android: Allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@string/app_name"        android: Theme= "@style/apptheme" >        <activity            android:name= "com.wxl.ninebox.MainActivity"            android: Label= "@string/app_name" >            <intent-filter>                <action android:name= " Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>    </application></manifest>

Mylayout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Background= "#fffccc" android:id= "@+id/mylayout" >        <imageview         android:id= "@+id/imageview"        Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:scaletype= "CenterCrop "        android:layout_gravity=" center_horizontal "        android:background=" @drawable/ic_launcher "/>    < TextView         android:id= "@+id/textview"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:layout_gravity=" bottom|center_horizontal "        android:layout_marginbottom=" 5DP "        android:textcolor= "#000000"        android:textsize= "12SP"/></framelayout>
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:gravity= "center"    tools:context= ". Mainactivity ">        <gridview         android:id=" @+id/gridview "         android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:numcolumns= "3"        android:verticalspacing= "30dip        " android:horizontalspacing= "10dip"        android:layout_gravity= "center"        >    </gridview></ Linearlayout>
Mainactivity.java

Package Com.wxl.ninebox;import Java.util.arraylist;import Java.util.hashmap;import android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.Window; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.gridview;import Android.widget.imageview;import Android.widget.textview;import Android.app.activity;import Android.content.context;import Android.graphics.Color; public class Mainactivity extends Activity {gridview GridView; View view; Myadapter Myadapter; Arraylist


Android UI Programming (1)--nine Gongge (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.