package com.bwie.adapter;
import java.util.List;
import com.bwie.bean.Data;
import com.bwie.bean.Image_list;
import com.bwie.bean.Large_image_list;
import com.bwie.bean.Middle_image;
import com.bwie.topnews.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Data> list;
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 NO_IMAGE = 4;
public MyAdapter(Context context, List<Data> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getItemViewType(int position) {
Data data = list.get(position);
List<Image_list> image_list = data.getImage_list();
List<Large_image_list> large_image_list = data.getLarge_image_list();
if (data.getHas_video() != null) {
if (data.getHas_video()) {
return VIDEO;
} else if (data.getHas_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 NO_IMAGE;
}
} else {
return NO_IMAGE;
}
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public View getView(int position, View convertview, ViewGroup arg2) {
ViewHolderNoImage noImage = null;
ViewHolderOneLargeImage oneLargeImage = null;
ViewHolderOneSmallImage oneSmallImage = null;
ViewHolderThreeImage threeImage = null;
ViewHolderVideo video = null;
int type = getItemViewType(position);
Data data = list.get(position);
if (convertview == null) {
switch (type) {
case VIDEO:
convertview = View.inflate(context, R.layout.listview_itemfour,
null);
video = new ViewHolderVideo();
video.tv_vedio_title = (TextView) convertview
.findViewById(R.id.lv_four_title);
// video.tv_title_news=(TextView)
// convertview.findViewById(R.id.textview_listview_news_video_title);
// video.sv=(SurfaceView)
// convertview.findViewById(R.id.sv_video);
findTextViews(video, convertview);
convertview.setTag(video);
break;
case ONE_SMALL_IMAGE:
convertview = View.inflate(context,
R.layout.listview_itemthree, null);
oneSmallImage = new ViewHolderOneSmallImage();
oneSmallImage.tv_title_news = (TextView) convertview
.findViewById(R.id.lv_three_title);
oneSmallImage.iv_onesmallimage = (ImageView) convertview
.findViewById(R.id.lv_three_ima);
findTextViews(oneSmallImage, convertview);
convertview.setTag(oneSmallImage);
break;
case ONE_LARGE_IMAGE:
convertview = View.inflate(context, R.layout.listview_itemtwo,
null);
oneLargeImage = new ViewHolderOneLargeImage();
oneLargeImage.tv_title_news = (TextView) convertview
.findViewById(R.id.lv_two_title);
oneLargeImage.iv_onelargeimage = (ImageView) convertview
.findViewById(R.id.lv_two_image);
findTextViews(oneLargeImage, convertview);
convertview.setTag(oneLargeImage);
break;
case THREE_IMAGE:
convertview = View.inflate(context, R.layout.listview_itemone,
null);
threeImage = new ViewHolderThreeImage();
threeImage.tv_title_news = (TextView) convertview
.findViewById(R.id.lv_one_title);
threeImage.iv_threeimage_one = (ImageView) convertview
.findViewById(R.id.lv_one_ima1);
threeImage.iv_threeimage_two = (ImageView) convertview
.findViewById(R.id.lv_one_ima2);
threeImage.iv_threeimage_three = (ImageView) convertview
.findViewById(R.id.lv_one_ima3);
findTextViews(threeImage, convertview);
convertview.setTag(threeImage);
break;
case NO_IMAGE:
convertview = View.inflate(context, R.layout.listview_itemfour,
null);
noImage = new ViewHolderNoImage();
noImage.tv_title_news = (TextView) convertview
.findViewById(R.id.lv_four_title);
findTextViews(noImage, convertview);
convertview.setTag(noImage);
break;
default:
break;
}
} else {
switch (type) {
case NO_IMAGE:
noImage = (ViewHolderNoImage) convertview.getTag();
break;
case VIDEO:
video = (ViewHolderVideo) convertview.getTag();
break;
case ONE_LARGE_IMAGE:
oneLargeImage = (ViewHolderOneLargeImage) convertview.getTag();
break;
case ONE_SMALL_IMAGE:
oneSmallImage = (ViewHolderOneSmallImage) convertview.getTag();
break;
case THREE_IMAGE:
threeImage = (ViewHolderThreeImage) convertview.getTag();
break;
default:
break;
}
}
switch (type) {
case NO_IMAGE:
setTextViewInfo(noImage, data);
noImage.tv_title_news.setText(data.getTitle());
break;
case VIDEO:
// setTextViewInfo(video, data);
// SurfaceHolder holder = video.sv.getHolder();
// holder.setType(SurfaceView.);
// surfaceHolder=surface.getHolder(); //SurfaceHolder是SurfaceView的控制接口
// surfaceHolder.addCallback(this);
// //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
// surfaceHolder.setFixedSize(320, 220); //显示的分辨率,不设置为视频默认
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //Surface类型
//
// play.setOnClickListener(new OnClickListener(){
// @Override
// public void onClick(View v) {
// player.start();
// }});
// pause.setOnClickListener(new OnClickListener(){
// @Override
// public void onClick(View v) {
// player.pause();
// }});
// stop.setOnClickListener(new OnClickListener(){
// @Override
// public void onClick(View v) {
// player.stop();
// }});
// }
//
// @Override
// public void surfaceChanged(SurfaceHolder arg0, int arg1, int
// arg2, int arg3) {
// }
//
// @Override
// public void surfaceCreated(SurfaceHolder arg0) {
// //必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
// player=new MediaPlayer();
// player.setAudioStreamType(AudioManager.STREAM_MUSIC);
// player.setDisplay(surfaceHolder);
// //设置显示视频显示在SurfaceView上
// try {
// player.setDataSource("/sdcard/3.mp4");
// player.prepare();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
video.tv_vedio_title.setText(data.getTitle());
break;
case ONE_LARGE_IMAGE:
setTextViewInfo(oneLargeImage, data);
oneLargeImage.tv_title_news.setText(data.getTitle());
ImageLoader.getInstance().displayImage(
data.getLarge_image_list().get(0).getUrl(),
oneLargeImage.iv_onelargeimage);
break;
case ONE_SMALL_IMAGE:
setTextViewInfo(oneSmallImage, data);
oneSmallImage.tv_title_news.setText(data.getTitle());
ImageLoader.getInstance().displayImage(
data.getMiddle_image().getUrl(),
oneSmallImage.iv_onesmallimage);
break;
case THREE_IMAGE:
setTextViewInfo(threeImage, data);
threeImage.tv_title_news.setText(data.getTitle());
ImageLoader.getInstance().displayImage(
data.getImage_list().get(0).getUrl(),
threeImage.iv_threeimage_one);
ImageLoader.getInstance().displayImage(
data.getImage_list().get(1).getUrl(),
threeImage.iv_threeimage_two);
ImageLoader.getInstance().displayImage(
data.getImage_list().get(2).getUrl(),
threeImage.iv_threeimage_three);
break;
default:
break;
}
return convertview;
}
/**
* 找到通用的控件
*
* @param vh
* @param convertview
*/
public void findTextViews(ViewHolder vh, View convertview) {
vh.tv_label_news = (TextView) convertview
.findViewById(R.id.textView_listview_news_label);
vh.tv_comment_news = (TextView) convertview
.findViewById(R.id.textView_listview_news_comment);
vh.tv_publishtime_news = (TextView) convertview
.findViewById(R.id.textView_listview_news_publishtime);
vh.tv_source_news = (TextView) convertview
.findViewById(R.id.textView_listview_news_source);
vh.btn_list_more = (ImageView) convertview
.findViewById(R.id.btn_listview_more);
}
public void setTextViewInfo(ViewHolder vh, Data data) {
vh.tv_comment_news.setText(data.getComment_count() + "评论");
if (!("热".equals(data.getLabel()))) {
vh.tv_label_news.setTextColor(Color.RED);
}
vh.tv_label_news.setText(data.getLabel());
vh.tv_publishtime_news.setText(getTime(data.getPublish_time()));
vh.tv_source_news.setText(data.getSource());
// 按钮的点击事件
vh.btn_list_more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyAlertDialog();
}
});
}
public void MyAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.create();
builder.show();
}
class ViewHolderNoImage extends ViewHolder {
}
class ViewHolderOneSmallImage extends ViewHolder {
ImageView iv_onesmallimage;
}
class ViewHolderOneLargeImage extends ViewHolder {
ImageView iv_onelargeimage;
}
class ViewHolderThreeImage extends ViewHolder {
ImageView iv_threeimage_one;
ImageView iv_threeimage_two;
ImageView iv_threeimage_three;
}
class ViewHolderVideo extends ViewHolder {
TextView tv_vedio_title;
SurfaceView sv;
}
class ViewHolder {
TextView tv_title_news;
TextView tv_label_news;
TextView tv_source_news;
TextView tv_publishtime_news;
TextView tv_comment_news;
ImageView btn_list_more;
}
/**
* 时间
*
* @param time
* @return
*/
public String getTime(int time) {
int second = time / 1000;
if (second > 60) {
int minute = second / 60;
if (minute > 60) {
int hour = minute / 60;
if (hour > 24) {
int day = hour / 24;
if (day > 24) {
return "很久前";
} else {
return day + "天前";
}
} else {
return hour + "小时前";
}
} else {
return minute + "分钟前";
}
} else {
return second + "秒前";
}
}
}
ListView的多布局
最新推荐文章于 2018-06-19 17:42:02 发布