GridView和RecyclerView刚说了Recyclerview,现在说下GridView;
直接上代码吧,代码很简单,,实现原理和Listview基本一样
String []name = {"1","2","3","4","5","6","7","8","9"}; List<Map<String,Object>>list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < name.length; i++) { Map<String,Object>map = new HashMap<>(); map.put("text",name[i]); list.add(map); } gridView = (GridView) findViewById(R.id.gridView); gridView.setAdapter(new SimpleAdapter(context,list,R.layout.item, new String[]{"text"},new int[]{R.id.tv_item}));
就这几行。。。。连适配器都写完了。。。
内容有点少,再把布局写上吧。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GridView" android:layout_margin="10dp" android:textSize="25sp" android:textColor="#000000" android:layout_gravity="center"/> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="5" android:padding="1dp" android:background="#000000"> </GridView> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_item" android:layout_width="match_parent" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="1dp" android:background="#ffffff" android:gravity="center" android:singleLine="true" /> </LinearLayout>