package com.bwie.adapter;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.bean.Tuice.Root;
import com.bwie.bean.Tuice.Root.DataEntity;
import com.bwie.main.newtopnews.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
/**
* Created by 开森De高先森! on 2016/6/14.
*/
public class Frag_lv_Adapter extends BaseAdapter {
private Context context;
public LinkedList<DataEntity> data;
private final int VIDEO = 0;
private final int ONE_SMALL_IMAGE = 1;
private final int ONE_LARGE_IMAGE = 2;
private final int THREE_IMAGE = 3;
private final int NOT_IMAGE = 4;
public Frag_lv_Adapter(Context context, LinkedList<DataEntity> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
/*获得当前条目的种类*/
@Override
public int getItemViewType(int position) {
// DataEntity root = data.get(position);
// LinkedList<ThridImage> image_list = root.getImage_list();
// LinkedList<Large_image> large_image_list = root.getLarge_image_list();
// /*判断如果包含的东西与布局文件对应就返回0*/
// if (root.isHas_video()) {
// return VIDEO;
// } else if (root.isHas_image()) {
// if (image_list.size() != 0) {
// if (image_list.size() >= 3) {
// return THREE_IMAGE;
// } else {
// return ONE_SMALL_IMAGE;
// }
// } else if (large_image_list != null) {
// if (large_image_list.size() != 0) {
// return ONE_LARGE_IMAGE;
// } else {
// return ONE_SMALL_IMAGE;
// }
// } else {
// return ONE_SMALL_IMAGE;
// }
// } else {
// return NOT_IMAGE;
// }
DataEntity dataRoot = data.get(position);
if (dataRoot.isHas_video()) {
return VIDEO;
} else if (dataRoot.isHas_image()) {
if (dataRoot.getImage_list().size() != 0) {
if (dataRoot.getImage_list().size() >= 3) {
return THREE_IMAGE;
}
} else if (dataRoot.getLarge_image_list() != null) {
if (dataRoot.getLarge_image_list().size() != 0) {
return ONE_LARGE_IMAGE;
}
} else if (dataRoot.getMiddle_image() != null) {
if (dataRoot.getMiddle_image().getUrl() != null) {
return ONE_SMALL_IMAGE;
}
} else {
return NOT_IMAGE;
}
}
return NOT_IMAGE;
}
@Override
public int getViewTypeCount() {
return 5;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
DataEntity dataEntity = data.get(position);
ViewHolder_Not holder_not = null;
ViewHolder_image holder_image = null;
ViewHolder_large_image large_image = null;
ViewHolder_three_image three_image = null;
ViewHolder_media_mp4 media_mp4 = null;
int type = getItemViewType(position);
if (view == null || holder_image == null || holder_image == null || large_image == null || three_image == null || media_mp4 == null) {
switch (type) {
case VIDEO:
media_mp4 = new ViewHolder_media_mp4();
view = View.inflate(context, R.layout.lv_five_media_mp4, null);
media_mp4.media_title = (TextView) view.findViewById(R.id.five_title);
media_mp4.media_iamge = (ImageView) view.findViewById(R.id.five_image);
media_mp4.media_hot = (TextView) view.findViewById(R.id.five_hot);
media_mp4.media_delete = (ImageView) view.findViewById(R.id.five_delete);
view.setTag(media_mp4);
break;
case ONE_SMALL_IMAGE:
holder_image = new ViewHolder_image();
view = View.inflate(context, R.layout.lv_item_one_image, null);
holder_image.one_title = (TextView) view.findViewById(R.id.one_title);
holder_image.one_source = (TextView) view.findViewById(R.id.one_source);
holder_image.one_comment = (TextView) view.findViewById(R.id.one_comment);
holder_image.one_image = (ImageView) view.findViewById(R.id.one_image);
holder_image.one_hot = (TextView) view.findViewById(R.id.one_hot);
holder_image.one_delete = (ImageView) view.findViewById(R.id.one_delete);
view.setTag(holder_image);
break;
case ONE_LARGE_IMAGE:
large_image = new ViewHolder_large_image();
view = View.inflate(context, R.layout.lv_item_three_large_image, null);
large_image.large_title = (TextView) view.findViewById(R.id.three_title);
large_image.large_source = (TextView) view.findViewById(R.id.three_source);
large_image.large_comment = (TextView) view.findViewById(R.id.three_comment);
large_image.large_time = (TextView) view.findViewById(R.id.three_time);
large_image.large_image = (ImageView) view.findViewById(R.id.three_large_image);
large_image.large_hot = (TextView) view.findViewById(R.id.three_hot);
large_image.large_delete = (ImageView) view.findViewById(R.id.three_delete);
view.setTag(large_image);
break;
case THREE_IMAGE:
three_image = new ViewHolder_three_image();
view = View.inflate(context, R.layout.lv_item_four_three_image, null);
three_image.three_title = (TextView) view.findViewById(R.id.four_title);
three_image.three_source = (TextView) view.findViewById(R.id.four_source);
three_image.three_comment = (TextView) view.findViewById(R.id.four_comment);
three_image.three_time = (TextView) view.findViewById(R.id.four_time);
three_image.three_image1 = (ImageView) view.findViewById(R.id.four_image_1);
three_image.three_image2 = (ImageView) view.findViewById(R.id.four_image_2);
three_image.three_image3 = (ImageView) view.findViewById(R.id.four_image_3);
three_image.three_hot = (TextView) view.findViewById(R.id.four_hot);
three_image.three_delete = (ImageView) view.findViewById(R.id.four_delete);
view.setTag(three_image);
break;
case NOT_IMAGE:
holder_not = new ViewHolder_Not();
view = View.inflate(context, R.layout.lv_item_two_not_image, null);
holder_not.not_title = (TextView) view.findViewById(R.id.two_title);
holder_not.not_source = (TextView) view.findViewById(R.id.two_source);
holder_not.not_comment = (TextView) view.findViewById(R.id.two_comment);
holder_not.not_time = (TextView) view.findViewById(R.id.two_time);
holder_not.not_hot = (TextView) view.findViewById(R.id.two_hot);
holder_not.not_delete = (ImageView) view.findViewById(R.id.two_delete);
view.setTag(holder_not);
break;
default:
Log.v("zzz", "出问题了哦!");
break;
}
} else {
switch (type) {
case VIDEO:
media_mp4 = (ViewHolder_media_mp4) view.getTag();
break;
case ONE_SMALL_IMAGE:
holder_image = (ViewHolder_image) view.getTag();
break;
case ONE_LARGE_IMAGE:
large_image = (ViewHolder_large_image) view.getTag();
break;
case THREE_IMAGE:
three_image = (ViewHolder_three_image) view.getTag();
break;
case NOT_IMAGE:
holder_not = (ViewHolder_Not) view.getTag();
break;
default:
break;
}
}
/*进行赋值*/
switch (type) {
case VIDEO:
media_mp4.media_title.setText(dataEntity.getTitle() + "");
if (dataEntity.getLarge_image_list() != null) {
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(dataEntity.getLarge_image_list().get(0).getUrl() + "", media_mp4.media_iamge);
}
SetText(media_mp4.media_hot, dataEntity.getLabel());
break;
case ONE_SMALL_IMAGE:
holder_image.one_title.setText(dataEntity.getTitle() + "");
holder_image.one_source.setText(dataEntity.getSource() + "");
holder_image.one_comment.setText("评论 " + dataEntity.getComment_count());
ImageLoader imageLoader1 = ImageLoader.getInstance();
imageLoader1.displayImage(dataEntity.getMiddle_image().getUrl() + "", holder_image.one_image);
SetText(holder_image.one_hot, dataEntity.getLabel());
break;
case ONE_LARGE_IMAGE:
large_image.large_title.setText(dataEntity.getTitle() + "");
large_image.large_source.setText(dataEntity.getSource() + "");
large_image.large_comment.setText("评论 " + dataEntity.getComment_count());
/*转换时间*/
int times1 = dataEntity.getPublish_time();
Date dates1 = new Date(times1);
SimpleDateFormat sdf1 = new SimpleDateFormat("HH");
String time1 = sdf1.format(dates1);
large_image.large_time.setText(time1 + "小时前");
ImageLoader imageLoader2 = ImageLoader.getInstance();
imageLoader2.displayImage(dataEntity.getLarge_image_list().get(0).getUrl() + "", large_image.large_image);
SetText(large_image.large_hot, dataEntity.getLabel());
break;
case THREE_IMAGE:
three_image.three_title.setText(dataEntity.getTitle() + "");
three_image.three_source.setText(dataEntity.getSource() + "");
three_image.three_comment.setText("评论 " + dataEntity.getComment_count());
/*转换时间*/
int times2 = dataEntity.getPublish_time();
Date dates2 = new Date(times2);
SimpleDateFormat sdf2 = new SimpleDateFormat("HH");
String time2 = sdf2.format(dates2);
three_image.three_time.setText(time2 + "小时前");
ImageLoader imageLoader3 = ImageLoader.getInstance();
imageLoader3.displayImage(dataEntity.getImage_list().get(0).getUrl() + "", three_image.three_image1);
imageLoader3.displayImage(dataEntity.getImage_list().get(1).getUrl() + "", three_image.three_image2);
imageLoader3.displayImage(dataEntity.getImage_list().get(2).getUrl() + "", three_image.three_image3);
SetText(three_image.three_hot, dataEntity.getLabel());
break;
case NOT_IMAGE:
holder_not.not_title.setText(dataEntity.getTitle() + "");
holder_not.not_source.setText(dataEntity.getSource() + "");
holder_not.not_comment.setText("评论 " + dataEntity.getComment_count());
/*转换时间*/
int times3 = dataEntity.getPublish_time();
Date dates3 = new Date(times3);
SimpleDateFormat sdf3 = new SimpleDateFormat("HH");
String time3 = sdf3.format(dates3);
holder_not.not_time.setText(time3 + "小时前");
SetText(holder_not.not_hot, dataEntity.getLabel());
break;
default:
break;
}
return view;
}
protected void SetText(TextView tv, String type) {
if (type == null) {
tv.setVisibility(View.GONE);
} else if ("置顶".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.darkgray);
} else if ("视频".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.update_action_btn_text);
} else if ("图片".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.sandybrown);
} else if ("推广".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.search_keyword_highlight);
} else if ("问答".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.left_drawer_offline_line);
} else if ("热".equals(type)) {
tv.setText(type);
tv.setBackgroundResource(R.color.red);
}
}
/* 优化部分 */
/*无图片优化*/
class ViewHolder_Not {
TextView not_title;
TextView not_source;
TextView not_comment;
TextView not_time;
TextView not_hot;
ImageView not_delete;
}
/*一个小图片优化*/
class ViewHolder_image {
TextView one_title;
ImageView one_image;
TextView one_source;
TextView one_comment;
TextView one_hot;
ImageView one_delete;
}
/*一个大图片优化*/
class ViewHolder_large_image {
TextView large_title;
ImageView large_image;
TextView large_source;
TextView large_comment;
TextView large_time;
TextView large_hot;
ImageView large_delete;
}
/*三个图片优化*/
class ViewHolder_three_image {
TextView three_title;
ImageView three_image1;
ImageView three_image2;
ImageView three_image3;
TextView three_source;
TextView three_comment;
TextView three_time;
TextView three_hot;
ImageView three_delete;
}
/*视频优化*/
class ViewHolder_media_mp4 {
TextView media_title;
ImageView media_iamge;
TextView media_hot;
ImageView media_delete;
}
}
ListView多布局适配器判断条件
最新推荐文章于 2019-09-05 20:18:52 发布