Introduction to this section:
This section introduces you to the second Adapter class control—GridView (grid view), as the name suggests, ListView is a list, and GridView is a display grid! He is a subclass of AbsListView like ListView! Many things are similar to ListView, in this section we will learn its basic usage~
1. Related attributes:
Here are some properties in GridView:
- android:columnWidth : set the width of the column
- android:gravity : the way components align
- android:horizontalSpacing : the spacing of each cell in the horizontal direction
- android:verticalSpacing : the spacing of each cell in the vertical direction
- android:numColumns : set the number of columns
- android:stretchMode : Set the stretching mode, the optional values are as follows: none : No stretching; spacingWidth : Stretch the space between elements columnWidth : Stretch only the table elements themselves spacingWidthUniform : Stretch both the spacing between elements and the space between them Interval air strikes
2. Example of use:
Let's use a simple example to familiarize yourself with the use of this control: (The Adapter we use here is the reusable BaseAdapter that we taught you to write in 2.5.0~)
Realized renderings :
Code implementation :
The first is the layout of the Item of the GridView: item_grid_icon.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 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 of MainActivity and Java code
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 to display 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(R.mipmap.iv_icon_5, "Icon 5")); mData.add(new Icon(R.mipmap.iv_icon_6, "Icon 6")); mData.add(new Icon(R.mipmap.iv_icon_7, "Icon 7")); mAdapter = new MyAdapter<Icon>(mData, R.layout.item_grid_icon) { @Override public void bindView(ViewHolder holder, Icon obj) { holder.setImageResource(R.id.img_icon, obj.getiId()); holder.setText(R.id.txt_icon, obj.getiName()); } }; grid_photo.setAdapter(mAdapter); grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "You clicked~" + position + "~item", Toast.LENGTH_SHORT).show(); } }); } }