自訂GridView/ListView資料來源

來源:互聯網
上載者:User

在開發中,我們常常會遇到比較複雜的GridView/ListView的布局,重新實現BaseAdapter不但能協助我們實現我們想要的布局效果,並且在綁定大資料量時也不會感覺有卡殼現象。記得以前用一個ListView直接去綁定手機內的連絡人Cursor(一百多號人),滑動的時候就會有卡的感覺。今天決定寫個Demo是因為在項目中可能會要實現這樣的一個效果:一個GridView中綁定4個ImageButton,有些按鈕在特定的情況下是停用,也就是Enable=false;並且不同的按鈕要擁有各自不同的點擊事件。

實現第一點好辦,添加一個標誌位boolean bl來控制按鈕的Enable狀態,實現第二點就是將View.onClickListener添加到List列表中

1、MyAdapter.java繼承自BaseAdapter

public class MyAdapter extends BaseAdapter{

private Context context;
private List<Map<String,Object>> list;
private LayoutInflater mInflater;
public MyAdapter(Context context,List<Map<String,Object>> list){
this.context=context;
this.list=list;
mInflater=LayoutInflater.from(this.context);
}
public int getCount() {
// TODO Auto-generated method stub
if(list!=null)
return list.size();
else
return 0;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
if(list!=null)
return list.get(position);
else
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@SuppressWarnings("unused")
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(holder==null){
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.gridview_item, null);
holder.ib=(ImageButton)convertView.findViewById(R.id.ib);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
//綁定點擊事件
holder.ib.setOnClickListener((OnClickListener) list.get(position).get("listen"));
//通過標誌位控制按鈕的Enable
if(Boolean.parseBoolean(list.get(position).get("bl").toString()))
holder.ib.setEnabled(true);
else
h

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.