public class NewsAdapter extends BaseAdapter {
Context context;
List<NewsDataBean.DataBean> list;
private final int TYPE_VIDEO = 0;
private final int TYPE_IMAGE_THREEE = 1;
private final int TYPE_IMAGE_ONE = 2;
private final int TYPE_NORMAL = 3;
public NewsAdapter(Context context, List<NewsDataBean.DataBean> 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;
}
/**
* 获取视图类型的数量...也就是有多少种条目类型
*
* @return
*/
@Override
public int getViewTypeCount() {
return 4;
}
/**
* 返回当前位置条目视图的类型....返回值是int值
*
* @param position ...当前位置
* @return
*/
@Override
public int getItemViewType(int position) {
//根据数据中的内容进行判断,...当前位置显示的是什么内容
if (list.get(position).isHas_video()){
//当前的条目时视频
return TYPE_VIDEO;
}else if (list.get(position).isHas_image()){
if (list.get(position).getImage_list().size()>=3){
//三张图片的样式
return TYPE_IMAGE_THREEE;
}else {
return TYPE_IMAGE_ONE;
}
}
//否则就是普通的条目
return TYPE_NORMAL;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//在写之前先判断一下当前属于哪种条目的类型
if (getItemViewType(i) == TYPE_VIDEO){
VideoHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_video,null);
holder = new VideoHolder();
holder.text_title = view.findViewById(R.id.text_title);
//holder
view.setTag(holder);
}else {
holder = (VideoHolder) view.getTag();
}
holder.text_title.setText(list.get(i).getTitle());
//...图片采用默认
}else if (getItemViewType(i) == TYPE_IMAGE_THREEE){
ImageThreeHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_three_image,null);
holder = new ImageThreeHolder();
holder.text_title = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (ImageThreeHolder) view.getTag();
}
holder.text_title.setText(list.get(i).getTitle());
}else if (getItemViewType(i) == TYPE_IMAGE_ONE){//一张图片的条目
ImageOneHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_image_one,null);
holder = new ImageOneHolder();
holder.text_title = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (ImageOneHolder) view.getTag();
}
holder.text_title.setText(list.get(i).getTitle());
}else {
//显示的是普通的条目
NormalHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_normal,null);
holder = new NormalHolder();
holder.text_title = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (NormalHolder) view.getTag();
}
holder.text_title.setText(list.get(i).getTitle());
}
return view;
}
//视频的
private class VideoHolder{
TextView text_title;
ImageView imageView;
}
private class ImageThreeHolder{
TextView text_title;
ImageView image_01;
ImageView image_02;
ImageView image_03;
}
private class ImageOneHolder{
TextView text_title;
ImageView imageView;
}
private class NormalHolder{
TextView text_title;
}
}