/**
* 2.重写适配器中getItemViewType方法...返回当前条目视图的类型,,,返回值是int值
*
* int返回值是有限制的,,,,例如getViewTypeCount返回的是4,,,那么他的取值只能是0,1,2,3
*
* 0表示第一种,,只有一个表题的条目样式
* 1表示图片在左边,文字在右边
* 2表示图片在右边,文字在左边
* 3表示文字在上边,图片在下面
*
* 实际开发中显示哪种条目的类型是根据接口数据中具体的那个字段值来决定的
*
* 现在,,例如...position%4
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if (position%4 == 0){
return TYTLE_ONLY;
}else if (position %4==1){
return IMAGE_LEFT;
}else if (position %4==2){
return IMAGE_RIGHT;
}
return IMAGE_BOTTOM;
}
-------------------------------------------------------------------------------------------------------------------------------------
public class MyAdapter extends BaseAdapter {
Context context;List<DataDataBean.ResultsBean> list;
private int TEXT_ONLY = 0;
private int IMAGE_TEXT = 1;
public MyAdapter(Context context, List<DataDataBean.ResultsBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (list.get(position).getImages() != null){
return IMAGE_TEXT;
}
return TEXT_ONLY;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (getItemViewType(i) == TEXT_ONLY){
TextHolder holder;
if (view == null){
view = View.inflate(context, R.layout.item_text_layout,null);
holder = new TextHolder();
holder.textView = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (TextHolder) view.getTag();
}
holder.textView.setText(list.get(i).getDesc());
}else if (getItemViewType(i)==IMAGE_TEXT){
ImageHolder holder;
if (view == null){
view = View.inflate(context, R.layout.item_iamge_layout,null);
holder = new ImageHolder();
holder.textView = view.findViewById(R.id.text_title);
holder.imageView = view.findViewById(R.id.image_view);
view.setTag(holder);
}else {
holder = (ImageHolder) view.getTag();
}
holder.textView.setText(list.get(i).getDesc());
//图片
Log.i("----",list.get(i).getImages().get(0)+"?imageView2/0/w/100");
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0)+"?imageView2/0/w/100",holder.imageView, ImageLoaderUtil.getRoundOption());
}
return view;
}
private class TextHolder{
TextView textView;
}
private class ImageHolder{
TextView textView;
ImageView imageView;
}
}