前言
GridView是本身就是个表格,我们见的哇比较多的是用它来显示列表图片,本Demo告诉你如何用GridView制作一个显示文本的表格,相当于MFC的CLIstCtrl控件功能。但是GridbView制作的表格一般都是等宽列的,要实现非等宽列的表格就得用ListView实现。
注:本文demo源码下载地址:http://download.youkuaiyun.com/detail/dxzysk/5752389
一、效果图
Demo制作的表格如下图所示:
界面不是很漂亮,大家按照需要修改一下
二、部分代码
package nishik.ncc.gridviewtest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import org.crazyit.gridview.GridViewTest;
//import org.crazyit.gridview.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
public class GridviewTableActivity extends Activity {
private static final int[] subAxisAxisNo = {
2201
,2202
,2203
,2204
,2205
,2206
,2207
,2208
,2209
,2210
,2211
,2212
,2213
,2214
,2215
,2216
,2217
,2218
,2219
,2220
,2221
,2222
,2223
,2224
,2225
,2226
,2227
,2228
,2229
,2230
,2231
,2232
,2233
,2234
,2235
,2236
,2237
,2238
,2239
,2240
,2241
,2242
,2243
,2244
,2245
,2246
,2247
,2248
,2249
,2250
,2251
,2252
,2253
,2254
,2255
,2256
,2257
,2258
,2259
,2260
,2261
,2262
,2263
,2264
,2265
,2273
,2274
,2275
,2276
,2281
,2282
,2283
,2284
,2285
,2286
,2287
,2288
,2289
,2290
,2291
,2292
,2293
,2294
,2295
,2296
,2297
,2298
,2299
,2300
,2301
,2302
,2303
,2304
,2305
,2306
,2307
,2308
,2309
,2310
,2311
,2312
,2313
,2314
,2315
,2316
,2317
,2318
,2319
,2320
,2321
,2322
,2323
,2324
,2325
,2326
,2327
,2328
,2329
,2330
,2331
,2332
,2333
,2334
,2335
,2336
,2337
,2338
,2339
,2340
,2341
,2342
,2343
,2344
,2345
,2346
,2347
,2348
,2349
,2350
,2351
,2352
,2353
,2354
,2355
,2356
,2357
,2358
,2359
,2360
,2361
,2362
,2363
,2364
,2365
,2366
,2367
,2368
,2369
,2370
,2371
,2372
,2373
,2374
,2375
,2376
,2377
,2378
,2379
,2380
,2381
,2382
,2383
,2384
,2385
,2386
,2387
,2388
,2389
,2390
,2391
,2392
,2393
,2394
,2395
,2396
,2397
,2398
,2399
,2400
,2401
,2402
,2403
,2404
,2405
,2406
,2407
,2408
,2409
,2410
,2411
,2412
,2413
,2414
,2415
,2416
,2417
,2418
,2419
,2420
,2421
,2422
,2423
,2424
,2425
,2426
,2427
,2428
,2429
,2430
,2431
,2432
,2433
,2434
,2435
,2436
,2437
,2438
,2439
,2440
,2441
,2442
,2443
,2444
,2445
,2446
,2447
,2448
,2449
,2450
,2451
,2452
,2453
,2454
,2455
,2456
};
private static final String[] subAxisAxisName = {
"SV001(PC1)"
,"SV002(PC2)"
,"SV003(PGN)"
,"SV004(PGN2)"
,"SV005(VGN1)"
,"SV006(VGN2)"
,"SV007(VIL)"
,"SV008(VIA)"
,"SV009(IQA)"
,"SV010(IDA)"
,"SV011(IQG)"
,"SV012(IDG)"
,"SV013(ILMT)"
,"SV014(ILMTsp)"
,"SV015(FFC)"
,"SV016(LMC1)"
,"SV017(SPEC1)"
,"SV018(PIT)"
,"SV019(RNG1)"
,"SV020(RNG2)"
,"SV021(OLT)"
,"SV022(OLL)"
,"SV023(OD1)"
,"SV024(INP)"
,"SV025(MTYP)"
,"SV026(OD2)"
,"SV027(SSF1)"
,"SV028(MSFT)"
,"SV029(VCS)"
,"SV030(IVC/Icx)"
,"SV031(OVS1)"
,"SV032(TOF)"
,"SV033(SSF2)"
,"SV034(SSF3)"
,"SV035(SSF4)"
,"SV036(PTYP)"
,"SV037(JL)"
,"SV038(FHz1)"
,"SV039(LMCD)"
,"SV040(LMCT/Icy)"
,"SV041(LMC2)"
,"SV042(OVS2)"
,"SV043(OBS1)"
,"SV044(OBS2)"
,"SV045(TRUB/Ib1)"
,"SV046(FHz2)"
,"SV047(EC1)"
,"SV048(EMGrt)"
,"SV049(PGN1sp)"
,"SV050(PGN2sp)"
,"SV051(DFBT)"
,"SV052(DFBN)"
,"SV053(OD3)"
,"SV054(ORE)"
,"SV055(EMGx)"
,"SV056(EMGt)"
,"SV057(SHGC)"
,"SV058(SHGCsp)"
,"SV059(TCNV)"
,"SV060(TLMT)"
,"SV061(DA1NO)"
,"SV062(DA2NO)"
,"SV063(DA1MPY)"
,"SV064(DA2MPY)"
,"SV065(TLC)"
,"SV073"
,"SV074"
,"SV075"
,"SV076"
,"SV081(SPEC2)"
,"SV082(SSF5)"
,"SV083(SSF6)"
,"SV084(SSF7)"
,"SV085(LMCk)"
,"SV086(LMCc)"
,"SV087(FHz4)"
,"SV088(FHz5)"
,"SV089(TQMAX Kq)"
,"SV090(TQMAX Kd)"
,"SV091(LMC4G)"
,"SV092(LMC4D)"
,"SV093(LMC4T)"
,"SV094"
,"SV095"
,"SV096"
,"SV097"
,"SV098"
,"SV099"
,"SV100"
,"SV101(TMA1)"
,"SV102(TMA2)"
,"SV103(TRES)"
,"SV104(FFR0)"
,"SV105(FFR1)"
,"SV106(PGM)"
,"SV107(FR)"
,"SV108(FA)"
,"SV109(ZR)"
,"SV110(ZA)"
,"SV111(TDM)"
,"SV112(IIF)"
,"SV113(SSF8)"
,"SV114(SSF9)"
,"SV115(SSF10)"
,"SV116(SSF11)"
,"SV117(RNG1ex)"
,"SV118"
,"SV119(FSP4)"
,"SV120(FSP5)"
,"SV121(MP Kpp)"
,"SV122(MP Kvp)"
,"SV123(MP Kvi)"
,"SV124"
,"SV125"
,"SV126"
,"SV127"
,"SV128"
,"SV129(Kwf)"
,"SV130(RPITS)"
,"SV131(DPITS)"
,"SV132"
,"SV133"
,"SV134"
,"SV135"
,"SV136"
,"SV137"
,"SV138"
,"SV139"
,"SV140"
,"SV141"
,"SV142"
,"SV143"
,"SV144"
,"SV145"
,"SV146"
,"SV147"
,"SV148"
,"SV149"
,"SV150"
,"SV151"
,"SV152"
,"SV153"
,"SV154"
,"SV155"
,"SV156"
,"SV157"
,"SV158"
,"SV159"
,"SV160"
,"SV161"
,"SV162"
,"SV163"
,"SV164"
,"SV165"
,"SV166"
,"SV167"
,"SV168"
,"SV169"
,"SV170"
,"SV171"
,"SV172"
,"SV173"
,"SV174"
,"SV175"
,"SV176"
,"SV177"
,"SV178"
,"SV179"
,"SV180"
,"SV181"
,"SV182"
,"SV183"
,"SV184"
,"SV185"
,"SV186"
,"SV187"
,"SV188"
,"SV189"
,"SV190"
,"SV191"
,"SV192"
,"SV193"
,"SV194"
,"SV195"
,"SV196"
,"SV197"
,"SV198"
,"SV199"
,"SV200"
,"SV201"
,"SV202"
,"SV203"
,"SV204"
,"SV205"
,"SV206"
,"SV207"
,"SV208"
,"SV209"
,"SV210"
,"SV211"
,"SV212"
,"SV213"
,"SV214"
,"SV215"
,"SV216"
,"SV217"
,"SV218"
,"SV219"
,"SV220"
,"SV221"
,"SV222"
,"SV223"
,"SV224"
,"SV225"
,"SV226"
,"SV227"
,"SV228"
,"SV229"
,"SV230"
,"SV231"
,"SV232"
,"SV233"
,"SV234"
,"SV235"
,"SV236"
,"SV237"
,"SV238(SSCFEED)"
,"SV239(SSCRPM)"
,"SV240"
,"SV241"
,"SV242"
,"SV243"
,"SV244"
,"SV245"
,"SV246"
,"SV247"
,"SV248"
,"SV249"
,"SV250"
,"SV251"
,"SV252"
,"SV253"
,"SV254"
,"SV255"
,"SV256"
};
private GridView m_paragrid;
//private GridView m_paranogrid;
private int colNum;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_paragrid = (GridView)findViewById(R.id.paragrid);
//m_paranogrid = (GridView)findViewById(R.id.paranogrid);
colNum = 6;
m_paragrid.setNumColumns(6);
//m_paranogrid.setNumColumns(4);
//创建一个List对象,List对象的元素是Map
List<Map<String, Object>> listItems
= new ArrayList<Map<String, Object>>();
setListItems(listItems,colNum - 2);
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new GridAdapter(this
, listItems
//使用/layout/cell.xml文件作为界面布局
, R.layout.cell
, new String[]{"cell"}
, new int[]{R.id.CellView});
//为GridView设置Adapter
m_paragrid.setAdapter(simpleAdapter);
//添加列表项被选中的监听器
m_paragrid.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int position , long id)
{
//显示当前被选中的图片
//switcher.setImageResource(imageIds[position % imageIds.length]);
}
public void onNothingSelected(AdapterView<?> parent){}
});
//添加列表项被单击的监听器
m_paragrid.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent
, View view, int position, long id)
{
//initCellBackground();
//显示被单击的图片的图片
//switcher.setImageResource(imageIds[position % imageIds.length]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void setListItems(List<Map<String, Object>> listItems ,int count)
{
for (int i = 0; i < 25/*subAxisAxisNo.length*/; i++)
{
Map<String, Object> listItemno = new HashMap<String, Object>();
listItemno.put("cell" , subAxisAxisNo[i]);
listItems.add(listItemno);
Map<String, Object> listItemname = new HashMap<String, Object>();
listItemname.put("cell" , subAxisAxisName[i]);
listItems.add(listItemname);
for (int j = 0; j < count; j++)
{
Map<String, Object> listItemvalue = new HashMap<String, Object>();
listItemvalue.put("cell" , 0);
listItems.add(listItemvalue);
}
}
}
public void initCellBackground(){
int iCount = m_paragrid.getChildCount();
for (int iIndex = 0; iIndex < iCount; iCount++){
if ((iIndex % colNum == 0) || (iIndex % colNum == 1)){
View lv = m_paragrid.getChildAt(iIndex);
if (lv instanceof ViewGroup){
View v =((ViewGroup)lv).getChildAt(0);
if (v instanceof TextView) {
v.setBackgroundColor(Color.RED);
}
}
}
}
}
class GridAdapter extends SimpleAdapter {
Context m_context;
public GridAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lView = super.getView(position, convertView, parent);
lView.setBackgroundColor(Color.RED);
if ((position % colNum == 0) || (position % colNum == 1)){
lView.setBackgroundColor(Color.rgb(200,255,255));
}else{
lView.setBackgroundColor(Color.WHITE);
}
return lView;
}
}
}