Android自定义GridView/ListView数据源
在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter 不但能帮助我们实现我们想要的布局效果,并且在绑定大数据量时也不会感觉有卡壳现象。记得以前用一个ListView直接去绑定手机内的联系人Cursor(一百多号人),滑动的时候就会有卡的感觉。今天决定写个Demo是因为在项目中可能会要实现这样的一个效果:一个GridView中绑定4个ImageButton,有些按钮在特定的情况下是不可用的,也就是Enable=false;并且不同的按钮要拥有各自不同的点击事件。
实现第一点好办,添加一个标志位boolean bl来控制按钮的Enable状态,实现第二点就是将View.onClickListener添加到List列表中
1、MyAdapter.Java继承自BaseAdapter
Java代码
1publicclassMyAdapterextendsBaseAdapter{
2
3privateContextcontext;
4privateList
5privateLayoutInflatermInflater;
6publicMyAdapter(Contextcontext,List
7this.context=context;
8this.list=list;
9mInflater=LayoutInflater.from(this.context);
10}
11publicintgetCount(){
12//TODOAuto-generatedmethodstub
13if(list!=null)
14returnlist.size();
15else
16return0;
17}
18
19publicObjectgetItem(intposition){
20//TODOAuto-generatedmethodstub
21if(list!=null)
22returnlist.get(position);
23else
24returnnull;
25}
26
27publiclonggetItemId(intposition){
28//TODOAuto-generatedmethodstub
29returnposition;
30}
31
32@SuppressWarnings("unused")
33publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
34//TODOAuto-generatedmethodstub
35ViewHolderholder=null;
36if(holder==null){
37holder=newViewHolder();
38convertView=mInflater.inflate(https://www.sodocs.net/doc/024726814.html,yout.gridview_item,null);
39holder.ib=(ImageButton)convertView.findViewById(R.id.ib);
40convertView.setTag(holder);
41}else{
42holder=(ViewHolder)convertView.getTag();
43}
44//绑定点击事件
45holder.ib.setOnClickListener((OnClickListener)list.get(position).get("li sten"));
46//通过标志位控制按钮的Enable
47if(Boolean.parseBoolean(list.get(position).get("bl").toString()))
48holder.ib.setEnabled(true);
49else
50holder.ib.setEnabled(false);
51holder.ib.setImageResource(Integer.parseInt(list.get(position).get("icon ").toString()));
52returnconvertView;
53}
54publicfinalclassViewHolder{
55publicImageButtonib;
56}
57}
2、绑定数据源
Java代码
58publicclassMainextendsActivity{
59/**Calledwhentheactivityisfirstcreated.*/
60privateGridViewgvtest;
61privateList
62@Override
63publicvoidonCreate(BundlesavedInstanceState){
64super.onCreate(savedInstanceState);
65setContentView(https://www.sodocs.net/doc/024726814.html,yout.main);
66
67findView();
68}
69
70/*
71*定义一个标志位bl控制按钮的Enable,同时为了让每个按钮都绑定自有的点击事件提前在数据源中间各自的事件实现
72*/
73privatevoidfindView(){
74gvtest=(GridView)findViewById(R.id.gvtest);
75list=newArrayList
76Map
77map=newHashMap
78map.put("bl",true);
79map.put("icon",R.drawable.menu_home_dis);
80View.OnClickListenerabenClick=newView.OnClickListener(){
81
82publicvoidonClick(Viewv){
83//TODOAuto-generatedmethodstub
84Toast.makeText(Main.this,"Youclickhomemenu",Toast.LENGTH_SHORT).show(); 85}
86};
87map.put("listen",abenClick);
88list.add(map);
89map=newHashMap
90map.put("bl",true);
91map.put("icon",R.drawable.menu_backward_dis);
92View.OnClickListenerbeanClick=newView.OnClickListener(){
93
94publicvoidonClick(Viewv){
95//TODOAuto-generatedmethodstub
96Toast.makeText(Main.this,"Youclickbackmenu",Toast.LENGTH_SHORT).show(); 97}
98};
99map.put("listen",beanClick);
100list.add(map);
101map=newHashMap
102map.put("bl",false);
103map.put("icon",R.drawable.menu_forward_dis);
104View.OnClickListenerweiboClick=newView.OnClickListener(){
105
106publicvoidonClick(Viewv){
107//TODOAuto-generatedmethodstub
108Toast.makeText(Main.this,"Youclickforwardmenu",Toast.LENGTH_SHORT).show( );
109}
110};
111map.put("listen",weiboClick);
112list.add(map);
113
114MyAdapteradapter=newMyAdapter(this,list);
115gvtest.setAdapter(adapter);
116}
117}
3、gridview_item.xml
Java代码
118
119 120xmlns:android="https://www.sodocs.net/doc/024726814.html,/apk/res/android" 121android:layout_width="wrap_content" 122android:layout_height="wrap_content" 123android:orientation="vertical"android:gravity="center"> 124 125 126android:layout_width="60dip"android:layout_height="60dip"> 127
4、Main.xml
Java代码
128
129 130android:orientation="horizontal" 131android:layout_width="fill_parent" 132android:layout_height="fill_parent" 133> 134 135 136android:layout_width="fill_parent"android:layout_height="wrap_content" 137android:numColumns="3"android:stretchMode="columnWidth" 138android:gravity="center"android:layout_gravity="center_horizontal"> 139