GridView 是Android开发中常用的一个控件,界面视图以表格的形式呈现。
实现GridView主要有以下三个步骤:
1、数据源,这里我们采用集合作为数据源。
2、适配器,我们采用SimpleAdapter()作为数据的适配器。
3、加载到视图界面。
首先在布局文件中添加一个GridView控件,属性设置如下:
<!--
android:numColumns="auto_fit" 每行的列数,自适应
android:horizontalSpacing="13dp" 每列之间的距离
android:verticalSpacing="13dp" 每行之间的距离
-->
<GridView
android:id="@+id/gv"
android:layout_marginTop="23dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:numColumns="3"
android:horizontalSpacing="13dp"
android:verticalSpacing="13dp"
>
</GridView>
接着回到java文件中,新建一个GridView引用、一个SimpleAdapter引用、一个List集合引用,图片资源image、文字资源text。接着通过findViewById()建立GridView对象 gv= (GridView) findViewById(R.id.gv);
创建ArrayList()对象new ArrayList<>();
创建SimpleAdapter()对象
adapter=new SimpleAdapter(this,getData(),R.layout.gridview,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
其中this是上下文、getData()是数据源、R.layout.gridview是适配的布局文件、String[]{“image”,”text”}是Map中的键值、new int[]{R.id.image,R.id.text}是gridview布局文件中的控件的id。
gridview布局文件的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android"/>
</LinearLayout>
getData()的代码,即将图片数组和文字数组加载到数据源中
private List<Map<String,Object>> getData(){
for(int i=0;i<image.length;i++){
Map<String,Object> map=new HashMap<>();
map.put("image",image[i]);
map.put("text",text[i]);
datalist.add(map);
}
return datalist;
}
最后用适配器将资源加载到视图界面。
实现点击事件监听的代码
public void onItemClick(AdapterView<?> parent, View view,int position,long id){
Toast.makeText(MainActivity.this,"I'm "+text[position],Toast.LENGTH_SHORT).show();
}
这样就完成GridView的使用。
完整代码如下:
package com.eyckwu.gridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private GridView gv;
private SimpleAdapter adapter;
private List<Map<String,Object>> datalist;
private int[] image={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
private String[] text={"android1","android2","android3","android4","android5","android6",
"android7","android8","android9","android10","android11","android12",};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv= (GridView) findViewById(R.id.gv);
datalist=new ArrayList<>();
adapter=new SimpleAdapter(this,getData(),R.layout.gridview,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
gv.setAdapter(adapter);
gv.setOnItemClickListener(this);
}
private List<Map<String,Object>> getData(){
for(int i=0;i<image.length;i++){
Map<String,Object> map=new HashMap<>();
map.put("image",image[i]);
map.put("text",text[i]);
datalist.add(map);
}
return datalist;
}
public void onItemClick(AdapterView<?> parent, View view,int position,long id){
Toast.makeText(MainActivity.this,"I'm "+text[position],Toast.LENGTH_SHORT).show();
}
}