相信大家都会使用ListView加载相同的布局了,接下来这篇文章,想说一下,如果要加载多种不同的布局该怎么去实现?
其实很简单,与之不同的是 ,加载相同的布局,我们只需搭建一种布局模板即可,而如果要加载多种不同的布局,我们就得准备好这几种不同的布局模板 ,接下来 我们就用个实例来说一下,如何加载三种不同的布局?
之前我们加载同一种布局的时候,是没必须写这两个方法的,但在加载多种不同布局的时候,我们就必须在自定义的Adapter中重写这两个方法, getItemViewType(int position) 和getViewTypeCount()
实例如下:
分析 如下:
第一步: 自定义CustomAdapter,继承自BaseAdapter,并重写其抽象方法,尤其必须重写文中提到的那两个方法
- class CustomAdapter extends BaseAdapter {
- ArrayList<HashMap<String, Object>> lists;
- Context context;
- LinearLayout linearLayout = null;
- LayoutInflater inflater;
- TextView text;
- final int VIEW_TYPE = 3;
- final int TYPE_1 = 0;
- final int TYPE_2 = 1;
- final int TYPE_3 = 2;
- public CustomAdapter(Context context,
- ArrayList<HashMap<String, Object>> lists) {
- this.lists = lists;
- this.context = context;
- }
- @Override
- public int getCount() {
- return lists.size();
- }
- @Override
- public Object getItem(int position) {
- return lists.get(position);
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
-
- // 这个方法必须重写 ,这个item要加载什么 布局类型
- // 每个convertView都会调用此方法,获得当前所需要的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(context);
-
- // 这里使用switch 最好了,根据布局类型,来加载什么布局模板
- switch (type) {
- case TYPE_1:
- convertView = inflater.inflate(R.layout.item_type1,
- parent, false);
- holder1 = new ViewHolder1();
- holder1.name = (TextView) convertView
- .findViewById(R.id.tv_list1_name);
- holder1.age = (TextView) convertView
- .findViewById(R.id.tv_list1_age);
- convertView.setTag(holder1);
- break;
- case TYPE_2:
- convertView = inflater.inflate(R.layout.item_type2,
- parent, false);
- holder2 = new ViewHolder2();
- holder2.name = (TextView) convertView
- .findViewById(R.id.tv_list2_name);
- holder2.age = (TextView) convertView
- .findViewById(R.id.tv_list2_age);
- convertView.setTag(holder2);
- break;
- case TYPE_3:
- convertView = inflater.inflate(R.layout.item_type3,
- parent, false);
- holder3 = new ViewHolder3();
- holder3.name = (TextView) convertView
- .findViewById(R.id.tv_list3_name);
- holder3.age = (TextView) convertView
- .findViewById(R.id.tv_list3_age);
- convertView.setTag(holder3);
- 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来做判断最好了,根据加载时哪种布局,给这个布局上的控件赋值
- switch (type) {
- case TYPE_1:
- holder1.name.setText("" + (position + 1));
- holder1.age.setText(lists.get(position).get("age").toString());
- break;
- case TYPE_2:
- holder2.name.setText("" + (position + 1));
- holder2.age.setText(ls.get(position).get("age").toString());
- holder2.totaldistance2.setText(lists.get(position);
- break;
- case TYPE_3:
- holder3.name.setText("" + (position + 1));
- holder3.age.setText(lists.get(position).get("age").toString());
- break;
- }
- return convertView;
- }
- public class ViewHolder1 {
- TextView name;
- TextView age;
- }
- public class ViewHolder2 {
- TextView name;
- TextView age;
- }
- public class ViewHolder3 {
- TextView name;
- TextView age;
- }
- }
第二步:接下来 就是在ListView上显示这三种不同的布局了
- private CustomAdapter customAdapter;
- ArrayList<HashMap<String, Object>> lists;
- ListView listView;
- lists=new ArrayList<HashMap<String, Object>>();
- customAdapter=new CustomAdapter(this, lists);
- listView = (Listview) view.findViewById(R.id.lv_listView);
- listView.setAdapter(customAdapter);