实现效果:
image.png1、初始化GridView、适配器
private List<Map<String, Object>> data_list;
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
Map<String, Object> map4 = new HashMap<String, Object>();
map1.put("BookImg", icomImageDefault[0]);
map1.put("BookName", "测试1");
map1.put("pushImage", icomPushImage[0]);
map2.put("BookImg", icomImageDefault[0]);
map2.put("BookName", "测试2");
map2.put("pushImage", icomPushImage[0]);
map3.put("BookImg", icomImageDefault[0]);
map3.put("BookName", "测试3");
map3.put("pushImage", icomPushImage[0]);
map4.put("BookImg", icomImageDefault[0]);
map4.put("BookName", "测试4");
map4.put("pushImage", icomPushImage[0]);
data_list = new ArrayList<Map<String, Object>>();
data_list.add(map1);
data_list.add(map2);
data_list.add(map3);
data_list.add(map4);
GridView gridView= (GridView ) findViewById(R.id.grid_view);
//新建适配器
String [] from ={"BookImg","BookName","pushImage"};
int [] to = {R.id.item_image,R.id.item_name,R.id.item_pushimage};
SimpleAdapter sim_adapter = new SimpleAdapter(getActivity(),data_list, R.layout.item_shouyelistview, from, to);
//配置适配器
gridView.setAdapter(sim_adapter);
2、GridView点击事件
//点击事件
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//position:个数
System.out.println("第" + position + "个");
}
});
3、GridView刷新UI
//刷新页面
gridView.setAdapter(sim_adapter);
sim_adapter.notifyDataSetChanged();
4、UI页面demo.xml代码
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gv_View"
android:layout_width="wrap_content"
android:layout_height="760dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
//横向列数
android:numColumns="5"
//每个宽度
android:columnWidth="164dp"
android:stretchMode="columnWidth"
//横纵间隔大小
android:verticalSpacing="40dp"
android:horizontalSpacing="40dp"
>
</GridView>
</RelativeLayout>
5、GridViewitem的布局代码itemGridView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/item_pushimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_gravity="center"
/>
</LinearLayout>
网友评论