优化
public class MyAdapter extends BaseAdapter {
private Context context;
private List<JsonBean.ResultBean.DataBean> data;
public MyAdapter(Context context, List<JsonBean.ResultBean.DataBean> data) {
this.data=data;
this.context=context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//条目布局的个数
@Override
public int getViewTypeCount() {
return 3;
}
//获取是哪个条目布局
@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.itext0=convertView.findViewById(R.id.itext0);
//存
convertView.setTag(holder0);
}else{
//取
holder0= (ViewHolder0) convertView.getTag();
}
//赋值
JsonBean.ResultBean.DataBean bean0= data.get(position);
holder0.itext0.setText(bean0.getTitle()+"");
//如果图片类型是集合则需要get0;
String s = data.get(position).getAlbums().get(0);
//如果不是直接调用
ImageLoader.getInstance().displayImage(s,holder0.image0);
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.image1);
holder1.itext1=convertView.findViewById(R.id.itext1);
//存
convertView.setTag(holder1);
}else{
//取
holder1= (ViewHolder1) convertView.getTag();
}
//赋值
JsonBean.ResultBean.DataBean bean1= data.get(position);
String ss = data.get(position).getAlbums().get(0);
ImageLoader.getInstance().displayImage(ss,holder1.image1);
holder1.itext1.setText(bean1.getTitle()+"");
break;
}
return convertView;
}
//有几个item 写几个viewholder
class ViewHolder0{
TextView itext0;
ImageView image0;
}
class ViewHolder1{
TextView itext1;
ImageView image1;
}
}
不优化
public class MyAdapter extends BaseAdapter {
public Context context;
public List<JsonBean.DataBean> list;
public MyAdapter(Context context, List<JsonBean.DataBean> list) {
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@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:
convertView= View.inflate(context, R.layout.item1,null);
TextView text1 = convertView.findViewById(R.id.text1);
ImageView image1 = convertView.findViewById(R.id.image1);
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),image1);
text1.setText(list.get(position).getNews_title());
break;
case 1:
convertView= View.inflate(context, R.layout.item2,null);
TextView text2 = convertView.findViewById(R.id.text2);
ImageView image2 = convertView.findViewById(R.id.image2);
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),image2);
text2.setText(list.get(position).getNews_title());
break;
}
return convertView;
}
}