<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">原创文章 ,转载请说明出处</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">ListVeiw的重要性不多说,总结今天一下使用LIstView加载两种Item样式的情况</span>
1)要加载两种item样式,两个item样式xml文件肯定需要准备好 个人觉得 id望文生义很重要,
2)绑定ListView控件,不多说, 接着initView()准备两个Item布局需要的数据
3)最关键的一步来了,重写BaseAdapter!!!!!!!!! 怎么重写呢???附上的例子实现ListView的第一个Item与之后的Item样式不同,直接上代码 ,附带详细注释
importandroid.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by CMS on 2016/8/10.
*/
public class TextAdapter extends BaseAdapter{
private class ViewHolder1{ //ListView中大海报模块ViewHolder
ImageView imageView1;
TextView textView1-1;
TextView textView1-2;
TextView textView1-3;
TextView textView1-4;
TextView textView1-5;
}
private class ViewHolder2{ //ListView下面子模块ViewHolder
ImageView imageView2;
TextView textView2-1;
TextView textView2-2;
TextView textView2-3;
TextView textView2-4;
}
private Context context; //存储上下文
private List<Map<String,Object>> listData; //存储数据源
private int[] resource; //存储item布局文件
private String[] keyForm; //存储数据key值
private int[] idForm; //存储对应控件Id
private LayoutInflater inflater; //用来找res/layout/下的xml布局文件,并且实例化
private ViewHolder1 viewHolder1=null;
private ViewHolder2viewHolder2=null;
public TextAdapter(Context context,List<Map<String,Object>>listData, int[]resource, String[] keyForm, int[] idForm){
this.context=context;
this.listData=listData;
this.resource=new int[resource.length];
this.keyForm=new String[keyForm.length];
this.idForm=new int[idForm.length];
//调用系统方法拷贝数组
System.arraycopy(keyForm,0,this.keyForm,0,keyForm.length);
System.arraycopy(idForm,0,this.idForm,0,idForm.length);
System.arraycopy(resource,0,this.resource,0,resource.length);
inflater=(LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount(){
return listData.size();
}
@Override
public ObjectgetItem(int position) {
return listData.get(position);
}
@Ov