案例一:
MainActivity布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.mrzhao.exam1demo.MainActivity"> android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:numColumns="3">
MainActivity文件:
public class MainActivity extends AppCompatActivity { private GridView girdView; private Listentities = new ArrayList<>(); private Gson gson; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); girdView = (GridView) findViewById(R.id.gridView); // 初始化数据 initData(); gson = new Gson(); // 转成 json String json = gson.toJson(entities); // 解析数组 Type type = new TypeToken >() { }.getType(); entities = gson.fromJson(json, type); //创建适配器 myAdapter =new MyAdapter(entities,this); girdView.setAdapter(myAdapter); } private void initData() { CustomEntity en1 = new CustomEntity("图片1", R.mipmap.ic_launcher); CustomEntity en2 = new CustomEntity("阿苏", R.mipmap.aaa); CustomEntity en3 = new CustomEntity("请问", R.mipmap.bbb); CustomEntity en4 = new CustomEntity("是 v 的", R.mipmap.ccc); CustomEntity en5 = new CustomEntity("儿童", R.mipmap.aaa); CustomEntity en6 = new CustomEntity("表示层v", R.mipmap.bbb); CustomEntity en7 = new CustomEntity("沃尔特", R.mipmap.ccc); CustomEntity en8 = new CustomEntity("电饭锅", R.mipmap.ic_launcher_round); CustomEntity en9 = new CustomEntity("体育", R.mipmap.ccc); CustomEntity en10 = new CustomEntity("体育", R.mipmap.aaa); entities.add(en1); entities.add(en2); entities.add(en3); entities.add(en4); entities.add(en5); entities.add(en6); entities.add(en7); entities.add(en8); entities.add(en9); entities.add(en10); } public void onClick(View view) { //模拟添加十条数据 initData(); // 刷新 myAdapter.notifyDataSetChanged(); } }
适配器:
public class MyAdapter extends BaseAdapter { private Listlist ; private Context context; private LayoutInflater inflater; public MyAdapter(List list, Context context) { this.list = list; this.context = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = new ViewHolder(); if (convertView == null){ convertView = inflater.inflate(R.layout.item_layout,parent,false); holder = new ViewHolder(); holder.imegView = convertView.findViewById(R.id.imageView); holder.textView = convertView.findViewById(R.id.textView); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } CustomEntity customEntity = list.get(position); holder.textView.setText(customEntity.getInfo()); holder.imegView.setImageResource(customEntity.getImg()); return convertView; } static class ViewHolder{ ImageView imegView; TextView textView; } }
条目布局:
xml version="1.0" encoding="utf-8"?>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"> android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" /> android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20sp" />
实体类文件:
public class CustomEntity { private String info; private int img; public CustomEntity(String info, int img) { this.info = info; this.img = img; } @Override public String toString() { return "CustomEntity{" + "info='" + info + '\'' + ", img=" + img + '}'; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } }
案例二:
MainActivity布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mrzhao.exam2demo.MainActivity"> android:id="@+id/gridView" android:layout_width="match_parent" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" android:layout_height="match_parent" android:numColumns="3">
MainActivity文件:
public class MainActivity extends AppCompatActivity { private GridView gridView; private Listlist = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); list.add("重庆"); list.add("上海"); list.add("广东"); list.add("成都"); list.add("西安"); list.add("厦门"); list.add("合肥"); list.add("银川"); list.add("广西"); MyAdapter myAdapter = new MyAdapter(list, this); gridView.setAdapter(myAdapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show(); } }); } }
适配器:
public class MyAdapter extends BaseAdapter { private Listlist; private Context context; private LayoutInflater inflater; private int[] bg = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED}; public MyAdapter(List list, Context context) { this.list = list; this.context = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = new ViewHolder(); if (convertView == null) { convertView = inflater.inflate(R.layout.item_layout, parent, false); holder = new ViewHolder(); holder.textView = convertView.findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(list.get(position)); convertView.setBackgroundColor(bg[position]); return convertView; } static class ViewHolder { TextView textView; } }
条目布局:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> android:id="@+id/textView" android:layout_marginTop="50dp" android:layout_marginBottom="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" />