1.这是多条目的适配器
package adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.w.huizong.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import bean.AndroidBean;
public class MyAdapter extends BaseAdapter {
private static final int ITEM_ONE =1 ;
private static final int ITEM_TWO = 2;
private static final int ITEM_THREE =3 ;
private Context context;
private List<AndroidBean.ResultsBean> resultsBeans;
private viewHolder1 holder1;
public MyAdapter(Context context, List<AndroidBean.ResultsBean> resultsBeans) {
this.context = context;
this.resultsBeans = resultsBeans;
}
@Override
public int getCount() {
return resultsBeans.size();
}
@Override
public Object getItem(int position) {
return resultsBeans.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public int getItemViewType(int position) {
List<String> images = resultsBeans.get(position).getImages();
if (images==null) {
return ITEM_ONE;
} else if (images.size() >2) {
return ITEM_TWO;
} else {
return ITEM_THREE;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
switch (type) {
case ITEM_ONE:
viewHolder1 holder1=null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_layout1, null);
holder1 = new viewHolder1();
holder1.desc = convertView.findViewById(R.id.text_desc);
holder1.time = convertView.findViewById(R.id.text_timer);
convertView.setTag(holder1);
} else {
holder1= (viewHolder1) convertView.getTag();
}
holder1.time.setText(resultsBeans.get(position).getCreatedAt());
holder1.desc.setText(resultsBeans.get(position).getDesc());
break;
case ITEM_TWO:
viewHolder2 holder2=null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_layout2, null);
holder2 = new viewHolder2();
holder2.desc = convertView.findViewById(R.id.text_desc1);
holder2.time = convertView.findViewById(R.id.text_timer1);
holder2.image1=convertView.findViewById(R.id.image_view1);
convertView.setTag(holder2);
} else {
holder2= (viewHolder2) convertView.getTag();
}
holder2.time.setText(resultsBeans.get(position).getCreatedAt());
try {
ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(0),holder2.image1);
} catch (Exception e) {
e.printStackTrace();
}
holder2.desc.setText(resultsBeans.get(position).getDesc());
break;
case ITEM_THREE:
viewHolder3 holder3=null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_layout3, null);
holder3 = new viewHolder3();
holder3.desc = convertView.findViewById(R.id.text_desc3);
holder3.time = convertView.findViewById(R.id.text_timer3);
holder3.image1=convertView.findViewById(R.id.image_view2);
holder3.image2=convertView.findViewById(R.id.image_view3);
holder3.image3=convertView.findViewById(R.id.image_view4);
convertView.setTag(holder3);
} else {
holder3= (viewHolder3) convertView.getTag();
}
holder3.time.setText(resultsBeans.get(position).getCreatedAt());
try {
ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(0),holder3.image1);
ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(1),holder3.image2);
ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(2),holder3.image3);
} catch (Exception e) {
e.printStackTrace();
}
holder3.desc.setText(resultsBeans.get(position).getDesc());
break;
}
return convertView;
}
class viewHolder{
TextView desc;
TextView time;
}
class viewHolder1 extends viewHolder{
}
class viewHolder2 extends viewHolder1{
ImageView image1;
}
class viewHolder3 extends viewHolder2{
ImageView image2;
ImageView image3;
}
}