未优化的Listview
@Override
public int getViewTypeCount() {
//视图个数
return 3;
}
@Override
public int getItemViewType(int position) {
//视图类型
return position%3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//根据类型加载不同的视图
switch (getItemViewType(position)){
case 0:
convertView = View.inflate(context,R.layout.modou1, null);
ImageView img1 = convertView.findViewById(R.id.img1);
TextView name1 = convertView.findViewById(R.id.name1);
name1.setText(list.get(position).getNews_title());
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),img1);
break;
case 1:
convertView = View.inflate(context,R.layout.modou2, null);
TextView name2 = convertView.findViewById(R.id.name2);
ImageView img2 = convertView.findViewById(R.id.img2);
name2.setText(list.get(position).getNews_title());
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),img2);
break;
case 2:
convertView = View.inflate(context,R.layout.modou3, null);
ImageView img3 = convertView.findViewById(R.id.img3);
TextView name3 = convertView.findViewById(R.id.name3);
TextView name4 = convertView.findViewById(R.id.name4);
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),img3);
name3.setText(list.get(position).getNews_title());
name4.setText(list.get(position).getNews_id());
break;
}
return convertView;
}
优化listview
//===========================================================================
@Override
public int getViewTypeCount() {//视图个数
return 2;
}
@Override
public int getItemViewType(int position) {//视图类型
return position % 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//根据类型加载不同的视图
switch (getItemViewType(position)) {
case 0:
ViewHolder0 holder0;
if (convertView==null){
convertView=View.inflate(context,R.layout.item1,null);
holder0=new ViewHolder0();
holder0.image0=convertView.findViewById(R.id.image0);
holder0.text0=convertView.findViewById(R.id.text0);
//存
convertView.setTag(holder0);
}else{
//取
holder0= (ViewHolder0) convertView.getTag();
}
//赋值
JsonBean.DataBean bean= list.get(position);
holder0.text0.setText(bean.getNews_id()+"");
holder0.image0.setImageResource(R.mipmap.see_true);
break;
case 1:
ViewHolder1 holder1;
if (convertView==null){
convertView=View.inflate(context,R.layout.item2,null);
holder1=new ViewHolder1();
holder1.image1=convertView.findViewById(R.id.image2);
holder1.text2=convertView.findViewById(R.id.text2);
holder1.text3=convertView.findViewById(R.id.text3);
//存
convertView.setTag(holder1);
}else{
//取
holder1= (ViewHolder1) convertView.getTag();
}
//赋值
JsonBean.DataBean bean1= list.get(position);
holder1.text2.setText(bean1.getNews_id()+"");
holder1.text3.setText(bean1.getNews_title()+"");
holder1.image1.setImageResource(R.mipmap.start_true);
break;
}
return convertView;
}
class ViewHolder0{
TextView text0;
ImageView image0;
}
class ViewHolder1{
TextView text3,text2;
ImageView image1;
}
}