<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); } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount(){ //重写getViewTypeCount()方法返回item样式的数量 这里实现ListView的第一个Item与之后的item的样式不同 return 2; } @Override public int getItemViewType(int position) { //返回item的样式类型 用在getView()方法中判断 if (position==0){ //如果是第一个item 放回类型0 return 0; }else{ //如果是第二个item 放回类型1 return 1; } } @Override public ViewgetView(int position, View convertView, ViewGroup parent){ if (getItemViewType(position)==0){ //如果item需要使用的样式类型为第一种 if (convertView!=null){ //判断是否缓存 viewHolder1= (ViewHolder1) convertView.getTag(); //converView不为空 通过tag找到缓存的布局 }else{ convertView=inflater.inflate(resource[0],null); //同步第一个item的样式布局文件的实例化布局 viewHolder1=new ViewHolder1(); //实例化第一个Item对应的VIE我Holder //获取idForm数组中的id数据 绑定控件 viewHolder1.imageView1= (ImageView) convertView.findViewById(idForm[0]); viewHolder1.textView1-1= (TextView) convertView.findViewById(idForm[1]); viewHolder1.textView1-2= (TextView) convertView.findViewById(idForm[2]); viewHolder1.textView1-3= (TextView) convertView.findViewById(idForm[3]); viewHolder1.textView1-4= (TextView) convertView.findViewById(idForm[4]); viewHolder1.textView1-5= (TextView) convertView.findViewById(idForm[5]); convertView.setTag(viewHolder1); } }else { //如果Item需要使用的样式为第二种 下面步骤同上 if (convertView!=null){ viewHolder2= (ViewHolder2) convertView.getTag(); }else{ convertView=inflater.inflate(resource[1],null); viewHolder2=new ViewHolder2(); viewHolder2.imageView2= (ImageView) convertView.findViewById(idForm[6]); viewHolder2.textView2-1= (TextView) convertView.findViewById(idForm[7]); viewHolder2.textView2-2= (TextView) convertView.findViewById(idForm[8]); viewHolder2.textView2-3= (TextView) convertView.findViewById(idForm[9]); viewHolder2.textView2-4= (TextView) convertView.findViewById(idForm[10]); convertView.setTag(viewHolder2); } } // HashMap<String,Object> map= (HashMap<String,Object>) listData.get(position); //通过位置获取listData中的map元素 if(getItemViewType(position)==0&&map!=null){ //当item需要加载的布局类型为第一种时 //通过keyFrorm获取各个数据相应的Key值 然后通过Key中在map中取出对应的数据 int imageView1= (Integer) map.get(keyForm[0]); String textView1-1= (String) map.get(keyForm[1]); String textView1-2= (String)map.get(keyForm[2]); String textView1-3= (String)map.get(keyForm[3]); //设置布局中控件要显示的视图 viewHolder1.imageView1.setImageResource(imageView1); viewHolder1.textView1-1.setText(textView1-1); viewHolder1.textView1-2.setText(textView1-2); viewHolder1.textView1-3.setText(textView1-3); //为特定的控件设置点击事件 viewHolder1.textView1-4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "报名参加", Toast.LENGTH_SHORT).show(); } }); viewHolder1.textView1-5.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(context, "查看详情", Toast.LENGTH_SHORT).show(); } }); }else if (getItemViewType(position)!=0&&map!=null){ //当item需要加载的布局类型为第二种时 其他步骤同上 int imageView2= (Integer) map.get(keyForm[4]); String textView2-1= (String) map.get(keyForm[5]); String textView2-2= (String)map.get(keyForm[6]); String textView2-3= (String)map.get(keyForm[7]); viewHolder2.imageView2.setImageResource(imageView2); viewHolder2.textView2-1.setText(textView2-1); viewHolder2.textView2-2.setText(textView2-2); viewHolder2.textView2-3.setText(textView2-3); viewHolder2.textView2-4.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(context, "查看详情", Toast.LENGTH_SHORT).show(); } }); } return convertView; } } 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); } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount(){ //重写getViewTypeCount()方法返回item样式的数量 这里实现ListView的第一个Item与之后的item的样式不同 return 2; } @Override public int getItemViewType(int position) { //返回item的样式类型 用在getView()方法中判断 if (position==0){ //如果是第一个item 放回类型0 return 0; }else{ //如果是第二个item 放回类型1 return 1; } } @Override public ViewgetView(int position, View convertView, ViewGroup parent){ if (getItemViewType(position)==0){ //如果item需要使用的样式类型为第一种 if (convertView!=null){ //判断是否缓存 viewHolder1= (ViewHolder1) convertView.getTag(); //converView不为空 通过tag找到缓存的布局 }else{ convertView=inflater.inflate(resource[0],null); //同步第一个item的样式布局文件的实例化布局 viewHolder1=new ViewHolder1(); //实例化第一个Item对应的VIE我Holder //获取idForm数组中的id数据 绑定控件 viewHolder1.imageView1= (ImageView) convertView.findViewById(idForm[0]); viewHolder1.textView1-1= (TextView) convertView.findViewById(idForm[1]); viewHolder1.textView1-2= (TextView) convertView.findViewById(idForm[2]); viewHolder1.textView1-3= (TextView) convertView.findViewById(idForm[3]); viewHolder1.textView1-4= (TextView) convertView.findViewById(idForm[4]); viewHolder1.textView1-5= (TextView) convertView.findViewById(idForm[5]); convertView.setTag(viewHolder1); } }else { //如果Item需要使用的样式为第二种 下面步骤同上 if (convertView!=null){ viewHolder2= (ViewHolder2) convertView.getTag(); }else{ convertView=inflater.inflate(resource[1],null); viewHolder2=new ViewHolder2(); viewHolder2.imageView2= (ImageView) convertView.findViewById(idForm[6]); viewHolder2.textView2-1= (TextView) convertView.findViewById(idForm[7]); viewHolder2.textView2-2= (TextView) convertView.findViewById(idForm[8]); viewHolder2.textView2-3= (TextView) convertView.findViewById(idForm[9]); viewHolder2.textView2-4= (TextView) convertView.findViewById(idForm[10]); convertView.setTag(viewHolder2); } } // HashMap<String,Object> map= (HashMap<String,Object>) listData.get(position); //通过位置获取listData中的map元素 if(getItemViewType(position)==0&&map!=null){ //当item需要加载的布局类型为第一种时 //通过keyFrorm获取各个数据相应的Key值 然后通过Key中在map中取出对应的数据 int imageView1= (Integer) map.get(keyForm[0]); String textView1-1= (String) map.get(keyForm[1]); String textView1-2= (String)map.get(keyForm[2]); String textView1-3= (String)map.get(keyForm[3]); //设置布局中控件要显示的视图 viewHolder1.imageView1.setImageResource(imageView1); viewHolder1.textView1-1.setText(textView1-1); viewHolder1.textView1-2.setText(textView1-2); viewHolder1.textView1-3.setText(textView1-3); //为特定的控件设置点击事件 viewHolder1.textView1-4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "报名参加", Toast.LENGTH_SHORT).show(); } }); viewHolder1.textView1-5.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(context, "查看详情", Toast.LENGTH_SHORT).show(); } }); }else if (getItemViewType(position)!=0&&map!=null){ //当item需要加载的布局类型为第二种时 其他步骤同上 int imageView2= (Integer) map.get(keyForm[4]); String textView2-1= (String) map.get(keyForm[5]); String textView2-2= (String)map.get(keyForm[6]); String textView2-3= (String)map.get(keyForm[7]); viewHolder2.imageView2.setImageResource(imageView2); viewHolder2.textView2-1.setText(textView2-1); viewHolder2.textView2-2.setText(textView2-2); viewHolder2.textView2-3.setText(textView2-3); viewHolder2.textView2-4.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(context, "查看详情", Toast.LENGTH_SHORT).show(); } }); } return convertView; } }
4)回到Activity 实例化TextAdapter 将准备好的数据传入 然后setAdapter