一.概念
GridView网格视图:带格子的ListView。
二.使用Adapter:常用BaseAdapter
三.属性
1.numColumns 列数:auto_fit,数值。
2.horizontalSpacing 水平间距
3.verticalSpacing 垂直间距
4.columnWidth 列宽
5.stretchMode 缩放方式
四.监听器
1. AdapterView.OnItemClickListener
void onItemClick(AdapterView<?> parent, View view, int position, long id),position 被点击的数据在集合中的索引。
2. AdapterView.OnItemLongClickListener()
boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id),返回true,表示事件已被消费。
3.API:notifyDataSetChanged() 通知数据改变并刷新视图。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.wang.testapp2.TestActivity10"> 11 12 <GridView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:id="@+id/gv_1" 16 android:numColumns="4" 17 android:stretchMode="columnWidth" 18 android:gravity="center" 19 android:horizontalSpacing="30dp" 20 android:verticalSpacing="30dp" 21 android:columnWidth="120dp" 22 ></GridView> 23 24 </LinearLayout> 25 26 .xml
1 package com.example.wang.testapp2; 2 3 import android.app.ActionBar; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.AbsListView; 10 import android.widget.AdapterView; 11 import android.widget.BaseAdapter; 12 import android.widget.GridView; 13 import android.widget.ImageView; 14 import android.widget.ListView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 public class TestActivity10 extends AppCompatActivity { 22 23 GridView gv_1; 24 25 // List<MyClass> lm; 26 List<Integer> liv; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_test10); 32 33 GridView gv_1=(GridView)findViewById(R.id.gv_1); 34 35 // lm=new ArrayList<>(); 36 // 37 // MyClass myClass=new MyClass(R.drawable.f1,"美食1"); 38 // 39 // lm.add(myClass); 40 // 41 // lm.add(new MyClass(R.drawable.f2,"美食2")); 42 // lm.add(new MyClass(R.drawable.f3,"美食3")); 43 // lm.add(new MyClass(R.drawable.f4,"美食4")); 44 // lm.add(new MyClass(R.drawable.f5,"美食5")); 45 // lm.add(new MyClass(R.drawable.f6,"美食6")); 46 // lm.add(new MyClass(R.drawable.f7,"美食7")); 47 // lm.add(new MyClass(R.drawable.f8,"美食8")); 48 // lm.add(new MyClass(R.drawable.f9,"美食9")); 49 // lm.add(new MyClass(R.drawable.f10,"美食10")); 50 // 51 // GridAdapter gridAdapter=new GridAdapter(); 52 // 53 // gv_1.setAdapter(gridAdapter); 54 55 56 57 58 59 //1 数据 图片的id 60 liv =new ArrayList<>(); 61 liv.add(R.drawable.f1); 62 liv.add(R.drawable.f2); 63 liv.add(R.drawable.f3); 64 liv.add(R.drawable.f4); 65 liv.add(R.drawable.f5); 66 liv.add(R.drawable.f6); 67 liv.add(R.drawable.f7); 68 liv.add(R.drawable.f8); 69 liv.add(R.drawable.f9); 70 liv.add(R.drawable.f10); 71 72 final ImageAdapter im=new ImageAdapter(); 73 74 gv_1.setAdapter(im); 75 76 gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 77 @Override 78 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 79 80 Toast.makeText(TestActivity10.this, "被点击的图片索引=" + position, Toast.LENGTH_SHORT).show(); 81 } 82 }); 83 84 //长按删除的监听器 85 gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 86 @Override 87 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 88 89 //1.删除数据 90 // 从集合里删除长按的数据 91 liv.remove(position); 92 93 //2.刷新视图,看到效果 94 // 通知适配器,数据源改变了 95 im.notifyDataSetChanged(); 96 97 Toast.makeText(TestActivity10.this, "删除了记录索引="+position, Toast.LENGTH_SHORT).show(); 98 99 return true;//事件已经被消费,不要在处理了 100 } 101 }); 102 } 103 104 // class MyClass 105 // { 106 // private int img; 107 // 108 // private String name; 109 // 110 // 111 // public int getImg() { 112 // return img; 113 // } 114 // 115 // public void setImg(int img) { 116 // this.img = img; 117 // } 118 // 119 // public String getName() { 120 // return name; 121 // } 122 // 123 // public void setName(String name) { 124 // this.name = name; 125 // } 126 // 127 // public MyClass(int img,String name) 128 // { 129 // this.img=img; 130 // this.name=name; 131 // } 132 // } 133 134 135 //适配器 136 137 class ImageAdapter extends BaseAdapter 138 { 139 @Override 140 public int getCount() { 141 return liv.size(); 142 } 143 144 @Override 145 public Object getItem(int position) { 146 return liv.get(position); 147 } 148 149 @Override 150 public long getItemId(int position) { 151 return 0; 152 } 153 154 @Override 155 public View getView(int position, View convertView, ViewGroup parent) { 156 157 //1. 得到数据 图片的id 158 Integer ivid = liv.get(position); 159 160 //2.准备View 161 if (convertView == null) { 162 convertView = new ImageView(TestActivity10.this); 163 } 164 165 //3.适配 166 // 转换 167 ImageView imageView = (ImageView) convertView; 168 169 imageView.setImageResource(ivid); 170 171 // imageView.setMaxWidth(70); 172 // imageView.setMaxHeight(70); 173 174 imageView.setLayoutParams(new GridView.LayoutParams(110, 110)); 175 176 177 return imageView; 178 179 // class GridAdapter extends BaseAdapter 180 // { 181 // @Override 182 // public int getCount() { 183 // return lm.size(); 184 // } 185 // 186 // @Override 187 // public Object getItem(int position) { 188 // return lm.get(position); 189 // } 190 // 191 // @Override 192 // public long getItemId(int position) { 193 // return 0; 194 // } 195 // 196 // @Override 197 // public View getView(int position, View convertView, ViewGroup parent) { 198 // MyClass myClass=lm.get(position); 199 // 200 // if (convertView==null) 201 // { 202 // Log.e("TAG","适配器getView="+position); 203 // 204 // convertView=View.inflate(TestActivity10.this,R.layout.listview_layout,null); 205 // } 206 // 207 // ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_3); 208 // imageView.setImageResource(myClass.getImg()); 209 // 210 // TextView textView=(TextView)convertView.findViewById(R.id.tv_9); 211 // textView.setText(myClass.getName()); 212 // 213 // 214 // return convertView; 215 // } 216 // } 217 }} 218 } 219 220 .java