30、自定义gridview
要想实现自定义gridview效果,有下边几个步骤:
1、定义grivew中的item的xml文件
假如item需要显示一个图片,图片下边显示文字:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="100dp" 4 android:layout_height="100dp" 5 android:orientation="vertical" 6 > 7 8 <Button 9 android:id="@+id/button_gridview_item" 10 android:layout_width="130dp" 11 android:layout_height="130dp" 12 android:text="" 13 14 android:focusable="false" 15 android:clickable="false" 16 17 android:textColor="#ffffff" 18 android:textSize="36sp" 19 /> 20 21 <TextView 22 android:id="@+id/tv_login_staff_name" 23 android:layout_width="130dp" 24 android:layout_height="30dp" 25 android:text="button" 26 27 android:focusable="false" 28 android:clickable="false" 29 android:textSize="15sp" 30 android:gravity="center" 31 android:textColor="#ffffff" 32 /> 33 34 </LinearLayout>
2、自定义adapter
我是extends了baseAdapter,然后在getView时,将图片从网络加载到button上。
注意点:这些getItem、getItemId、getCount等继承过来的方法,一定要记得再次赋值
1 public class login_gridview_adapter extends BaseAdapter{ 2 3 public static int selectPic = -1; 4 private Context context = null; 5 private List<login_staff> list = null; 6 private String tag = "adapter"; 7 8 public login_gridview_adapter(Context ctxt,List<login_staff> ls) 9 { 10 for(int i=0;i<ls.size();i++) 11 { 12 CommonUtils.LogWuwei(tag,"in adaper "+ls.get(i).name ); 13 } 14 15 context = ctxt; 16 list = ls; 17 } 18 19 20 21 @Override 22 public int getCount() { 23 // TODO Auto-generated method stub 24 return list.size(); 25 } 26 27 public void setNotifyDataChange(int id) { 28 selectPic = id; 29 super.notifyDataSetChanged(); 30 } 31 32 @Override 33 public Object getItem(int position) { 34 // TODO Auto-generated method stub 35 return list.get(position); 36 } 37 38 @Override 39 public long getItemId(int position) { 40 // TODO Auto-generated method stub 41 return position; 42 } 43 44 @Override 45 public View getView(int position, View convertView, ViewGroup parent) { 46 // TODO Auto-generated method stub 47 48 49 CommonUtils.LogWuwei(tag, "position is "+position); 50 51 LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 52 View grid = inflater.inflate(R.layout.grid_item, null); 53 54 55 if (convertView == null) { 56 grid = new View(context); 57 grid = inflater.inflate(R.layout.grid_item, null); 58 Button button = (Button) grid.findViewById(R.id.button_gridview_item); 59 String imgUrl = list.get(position).head; 60 login.bitmapUtils.display(button, imgUrl, login.bigPicDisplayConfig, null); 61 62 TextView tv = (TextView) grid.findViewById(R.id.tv_login_staff_name); 63 tv.setText(list.get(position).name); 64 65 } else { 66 grid = (View) convertView; 67 } 68 69 if(selectPic == position) 70 { 71 String imgUrl = list.get(position).head; 72 //convertView.setAnimation(new AnimationUtils().loadAnimation(login.ctxt,R.anim.unzoom_in)); 73 Message msg = new Message(); 74 msg.what = login.REMOVE_GRIDVIEWS; 75 login.handler.sendMessage(msg); 76 login.bitmapUtils.display(login.gridviewStaffList, imgUrl, login.bigPicDisplayConfig, null); 77 } 78 return grid; 79 } 80 81 82 83 }
3、activity中设置gridview的适配器为我们上一步自定义的adapter
1 gridviewStaffListAdapter = new login_gridview_adapter(ctxt, listLoginStaff); 2 3 gridviewStaffList.setAdapter(gridviewStaffListAdapter); 4 gridviewStaffList.setSelector(new ColorDrawable(Color.TRANSPARENT)); 5 gridviewStaffList.setOnItemClickListener(new OnItemClickListener() { 6 7 @Override 8 public void onItemClick(AdapterView<?> parent, View view, 9 int position, long id) { 10 // TODO Auto-generated method stub 11 MsgUtils.SendSingleMsg(new HandlerUtils(ctxt).handler, listLoginStaff.get(position).name,HandlerUtils.SHOW_TOAST); 12 gridviewStaffListAdapter.setNotifyDataChange(position); 13 positionStaffId = listLoginStaff.get(position).staff_id; 14 } 15 });
4、总结
要想改变gridview中数据,必须更新适配器(也就是设置过的adapter);那么要想设置适配器,必须设置适配器定义时传入的list,list经过add、remove等操作后,要想让
gridview跟着改变,需要手动更新一下,比如说这样:
gridviewStaffListAdapter.notifyDataSetChanged();