2.5.2 Basic Usage of GridView (Grid View)
Category Android Basic Tutorial
Introduction to This Section:
>
This section introduces the second adapter class control - GridView (Grid View). As the name suggests, while ListView is a list, GridView is for displaying grids! Like ListView, it is also a subclass of AbsListView! Many things are common with ListView, and in this section, we will learn about its basic usage~
1. Related Attributes:
Here are some attributes in GridView:
>
android:columnWidth: Set the width of the column
android:gravity: The alignment method for the component
android:horizontalSpacing: The spacing between each cell in the horizontal direction
android:verticalSpacing: The spacing between each cell in the vertical direction
android:numColumns: Set the number of columns
android:stretchMode: Set the stretch mode, with the following possible values: none: Do not stretch; spacingWidth: Stretch the spacing between elements columnWidth: Only stretch the table elements themselves spacingWidthUniform: Stretch both the element spacing and the spacing between them
2. Usage Example:
>
Let's get familiar with the use of this control through a simple example: (The Adapter we use here is directly the reusable BaseAdapter we taught in section 2.5.0~)
Implementation Effect Picture :
Code Implementation :
Firstly, the layout for the GridView's item: itemgridicon.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/img_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerInParent="true"
android:src="@mipmap/iv_icon_1" />
<TextView
android:id="@+id/txt_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_icon"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Hehe"
android:textSize="18sp" />
</RelativeLayout>
Then we write an entity 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;
}
}
Finally, the layout and Java code for MainActivity
activity_main.xml:
<RelativeLayout 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:padding="5dp"
tools:context=".MainActivity">
<!--numColumns sets how many are displayed per row-->
<GridView
android:id="@+id/grid_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" />
</RelativeLayout>
MainActivity.java:
``` public class MainActivity extends AppCompatActivity {
private Context mContext;
private GridView grid_photo;
private BaseAdapter mAdapter = null;
private ArrayList<Icon> 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<Icon>();
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(
1.8 Project-Related Analysis (Various Files, Resource Access)
2.5.2 Basic Usage of GridView (Grid View)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
[2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)](android-tutorial-
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (V)
8.3.14 Paint Several Enum/Constant Values and ShadowLayer Shadow Effect
[8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods](android-tutorial-canvas-api2