This blog discusses how to display a GridView. In the first two blogs, we used ListView. When displaying a ListView, we used the Adapter. The function of the Adapter is to adapt the data source to the list control. A ListAdapter is also required to display the GridView.
Let's start with activity_main.xml code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.zdk.gridview.MainActivity"> <GridView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gridview1" android:columnWidth="100dip" android:numColumns="auto_fit"></GridView> </RelativeLayout>
The above layout defines a GridView and sets the columnWidth to a fixed value. android:numColumns="auto_fit" means that the Android system determines how many columns the GridView should display.
The properties related to GridView will be listed at the bottom and introduced briefly. Next, let's look at the code. Here's mainactivity Java code
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.ListAdapter; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String[] myListItems={"To", "be", "or", "not", "to", "be","that", "is", "the", "question"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridView = (GridView) findViewById(R.id.gridview1); ListAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,myListItems); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, myListItems[i], Toast.LENGTH_SHORT).show(); } }); } }
See, this is the same routine. Define an array or list (myListItems array above), specify a layout for displaying each item (android.R.layout.simple_list_item_1 above), and finally instantiate a ListAdapter object and set it as the Adapter of GridView to display an array through GridView.
Next, an instance of an anonymous inner class is assigned to the gridView through the setOnItemClickListener method. The onItemClick method in the anonymous inner class will be called when each element is clicked.
Here are some properties related to GridView
- android:numColumns: specifies how many columns the GridView displays. It can be set to a specific number or auto_fit, which means that the Android system determines how many columns to display according to factors such as column width, interval between columns, screen size, etc.
- android:columnWidth: Specifies the width of each column
- Android: vertical spacing: Specifies the spacing between rows
- android:horizontalSpacing: Specifies the spacing between columns
- android:stretchMode: if android:columnWidth is set to auto_fit, and if there is extra space in the horizontal direction after allocating the columns, you can use this attribute to decide how to allocate the remaining space. There are two options to set. If it is set to columnWidth, the remaining space will be allocated to each column. If it is set to spacingWidth, the remaining space will be allocated to the spacing between columns.