Easy Tutorial
❮ The Different Of Jre And Jdk Verilog Time Delay ❯

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:

>


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(

❮ The Different Of Jre And Jdk Verilog Time Delay ❯