1在布局文件中
<FrameLayout 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"
tools:context="com.example.myfragment.fragment.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
//列的个数
android:numColumns="4"
//列的宽度
android:columnWidth="80dp"/>
</FrameLayout>
2在代码中,获取数据添加适配器
private void initData() {
int[] icons ={R.drawable.citycontact,R.drawable.gongjutai,R.drawable.gongwen,R.drawable.huiyiapply,R.drawable.huiyicailiao,R.drawable.lingdaohuodong,R.drawable.shezhi,R.drawable.youjian,R.drawable.zhuxiao,R.drawable.fujian,R.drawable.wengao};
String[] iconNames={"通讯录","工具台","公文","纪要","材料","活动","设置","邮件","注销","附件","文稿"};
List<Map<String,Object>> list = new ArrayList<>();
for(int i=0;i<icons.length;i++)
{
Map<String,Object> map = new HashMap<>();
map.put("icon",icons[i]);
map.put("name",iconNames[i]);
list.add(map);
}
gridview.setAdapter( new GridAdapter(getActivity(),list));
}
3适配器里的代码实现
class GridAdapter extends BaseAdapter {
private List<Map<String, Object>> list;
private Context context;
private LinearLayout container;
public GridAdapter(Context context, List<Map<String, Object>> list) {
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View vie = View.inflate(context, R.layout.item_grid ,null);
ImageView image = vie.findViewById(R.id.imag);
container = vie.findViewById(R.id.ll_container);
TextView text = vie.findViewById(R.id.tv);
image.setImageResource((int)list.get(i).get("icon"));
text.setText((String)(list.get(i).get("name")));
setLister(list.get(i));//如果数目不确定可以这种方式处理点击事件
return vie;
}
private void setLister(final Map<String, Object> stringObjectMap) {
container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,(String)(stringObjectMap.get("name")),Toast.LENGTH_SHORT).show();
}
});
}
}
如果数目明确可以用下面的点击事件
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (i){
case 0:
break;
case 1:
break;
}
}
});
item_grid布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="10dp"
android:id="@+id/ll_container"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:gravity="center"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>