GirdView程式碼分析

來源:互聯網
上載者:User

標籤:

 // 建立一個List對象,List對象的元素是Map

List<Map<String, Object>> listItems =
new ArrayList<Map<String, Object>>();

for (int i = 0; i < imageIds.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image", imageIds[i]);//為list對象設定圖片
listItems.add(listItem);//添加進組件
}

// 擷取顯示圖片的ImageView
imageView = (ImageView) findViewById(R.id.imageView);
// 建立一個SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,
listItems
// 使用/layout/cell.xml檔案作為介面布局
, R.layout.cell, new String[] { "image" },
new int[] { R.id.image1 });
grid = (GridView) findViewById(R.id.grid01);
// 為GridView設定Adapter
grid.setAdapter(simpleAdapter);

/***************以下兩個事件監聽器看似一樣將第二個刪去後顯示失敗,將第一個刪去後正常顯示。****
選中監聽器失效。。小編以為,本程式為點擊事件監聽,沒有選中的關係。。。。。

***************/
// 添加清單項目被選中的監聽器
grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
// 顯示當前被選中的圖片
imageView.setImageResource(imageIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
// 添加清單項目被單擊的監聽器
grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// 顯示被單擊的圖片
imageView.setImageResource(imageIds[position]);
}
});
/***************************************************************************/
}














GirdView程式碼分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.