因为listView很少使用了,怕忘记,做个保存!!!
public class ListViewAdapter extends BaseAdapter{
private Context context;
private List<ListDataDataBean.ResultsBean> data;
private final int ONLY_TITLE = 0;
private final int IMAGE_TITLE = 1;
public ListViewAdapter(Context context, List<ListDataDataBean.ResultsBean> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(data.get(position).getImages() == null){
return ONLY_TITLE;
}
return IMAGE_TITLE;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
int type = getItemViewType(i);
if (type == ONLY_TITLE){
OnlyTitleViewHolder holder;
if(view == null){
view = View.inflate(context, R.layout.listview_imagetitle, null);
holder = new OnlyTitleViewHolder();
holder.textView = view.findViewById(R.id.listview_textview);
view.setTag(holder);
}else{
holder = (OnlyTitleViewHolder) view.getTag();
}
holder.textView.setText(data.get(i).getDesc());
}else if(type == IMAGE_TITLE){
ImageTitleViewHolder holder;
if(view == null){
view = View.inflate(context, R.layout.listview_imagetitle, null);
holder = new ImageTitleViewHolder();
holder.textView = view.findViewById(R.id.listview_textview);
holder.imageView = view.findViewById(R.id.listview_imageview);
view.setTag(holder);
}else{
holder = (ImageTitleViewHolder) view.getTag();
}
holder.textView.setText(data.get(i).getDesc());
holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(data.get(i).getImages().get(0), holder.imageView, ImageLoaderUtil.initOptions());
}
return view;
}
private class OnlyTitleViewHolder{
TextView textView;
}
private class ImageTitleViewHolder{
TextView textView;
ImageView imageView;
}
}