Android ListView中 每一项都有不同的布局
实现代码
Adapter的代码
其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局
TYPE_1...是三种类型.
在使用不同布局的时候,getItemViewType和getViewType不能少,一般是不用这两个函数的重载的
class RunRankAdapter extends BaseAdapter { ArrayList<HashMap<String, Object>> ls; Context mContext; LinearLayout linearLayout = null; LayoutInflater inflater; TextView tex; final int VIEW_TYPE = 3; final int TYPE_1 = 0; final int TYPE_2 = 1; final int TYPE_3 = 2; public RunRankAdapter(Context context, ArrayList<HashMap<String, Object>> list) { ls = list; mContext = context; } @Override public int getCount() { return ls.size(); } @Override public Object getItem(int position) { return ls.get(position); } @Override public long getItemId(int position) { return 0; } // 每个convert view都会调用此方法,获得当前所需要的view样式 @Override public int getItemViewType(int position) { int p = position; if (p == 0) return TYPE_1; else if (p == 1) return TYPE_2; else return TYPE_3; } @Override public int getViewTypeCount() { return 3; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; ViewHolder3 holder3 = null; int type = getItemViewType(position); if (convertView == null) { inflater = LayoutInflater.from(mContext); // 按当前所需的样式,确定new的布局 switch (type) { case TYPE_1: convertView = inflater.inflate(R.layout.item_runrank1, parent, false); holder1 = new ViewHolder1(); holder1.rank1 = (TextView) convertView .findViewById(R.id.tv_list1_rank); holder1.time1 = (TextView) convertView .findViewById(R.id.tv_list1_time); convertView.setTag(holder1); break; case TYPE_2: convertView = inflater.inflate(R.layout.item_runrank2, parent, false); holder2 = new ViewHolder2(); holder2.rank2 = (TextView) convertView .findViewById(R.id.tv_list2_rank); holder2.time2 = (TextView) convertView .findViewById(R.id.tv_list2_time); convertView.setTag(holder2); break; case TYPE_3: convertView = inflater.inflate(R.layout.item_runrank3, parent, false); holder3 = new ViewHolder3(); holder3.rank3 = (TextView) convertView .findViewById(R.id.tv_list3_rank); holder3.time3 = (TextView) convertView .findViewById(R.id.tv_list3_time); convertView.setTag(holder3); break; default: break; } } else { switch (type) { case TYPE_1: holder1 = (ViewHolder1) convertView.getTag(); break; case TYPE_2: holder2 = (ViewHolder2) convertView.getTag(); break; case TYPE_3: holder3 = (ViewHolder3) convertView.getTag(); break; } } // 设置资源 switch (type) { case TYPE_1: holder1.rank1.setText("" + (position + 1)); holder1.time1.setText(ls.get(position).get("time").toString()); break; case TYPE_2: holder2.rank2.setText("" + (position + 1)); holder2.time2.setText(ls.get(position).get("time").toString()); holder2.totaldistance2.setText(ls.get(position) break; case TYPE_3: holder3.rank3.setText("" + (position + 1)); holder3.time3.setText(ls.get(position).get("time").toString()); break; } return convertView; } public class ViewHolder1 { TextView rank1; TextView time1; } public class ViewHolder2 { TextView rank2; TextView time2; } public class ViewHolder3 { TextView rank3; TextView time3; } }
使用方式:
声明并定义自己的Adapter,在ListView中设置他的Adapter
private RunRankAdapter runRankAdapter; ArrayList<HashMap<String, Object>> runRank; ListView lv_runRank; runRank = new ArrayList<HashMap<String, Object>>(); runRankAdapter = new RunRankAdapter(this, runRank); lv_runRank = (ListView) findViewById(R.id.lv_runRank); lv_runRank.setAdapter(runRankAdapter);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。