package com.example.adapter;
import android.content.Context;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.bean.DataInfo;
import com.example.day8_lianxi2.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by 杨柳 on 2018/7/13.
*/
public class MyAdapter extends BaseAdapter{
private List<DataInfo.ResultsBean> list;
private Context context;
public MyAdapter(List<DataInfo.ResultsBean> list, Context context) {
this.list = list;
this.context = context;
}
//添加到集合元素0的前面
public void add(List<DataInfo.ResultsBean> resultsBeans){
this.list.addAll(0,resultsBeans);
notifyDataSetChanged();
}
//在集合后面添加元素
public void loadMore(List<DataInfo.ResultsBean> resultsBeans){
this.list.addAll(resultsBeans);
notifyDataSetChanged();
}
@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 getItemViewType(int position) {
List<String> images = list.get(position).getImages();
if (images == null || images.size() == 0){
return 0 ;
}else if (images.size()>2){
return 3;
}else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
int itemViewType = getItemViewType(i);
switch (itemViewType){
case 0:
ViewHolde1 holder1 = null;
if (view == null ){
view = View.inflate(context, R.layout.list1_layout, null);
holder1 = new ViewHolde1();
holder1.text_title = view.findViewById(R.id.title);
view.setTag(holder1);
}else {
holder1 = (ViewHolde1) view.getTag();
}
holder1.text_title.setText(list.get(i).getCreatedAt());
break;
case 1:
ViewHolder2 holder2 = null;
if (view == null ){
view = View.inflate(context, R.layout.list2_layout, null);
holder2 = new ViewHolder2();
holder2.text_title = view.findViewById(R.id.title);
holder2.img1 = view.findViewById(R.id.img1);
view.setTag(holder2);
}else {
holder2 = (ViewHolder2) view.getTag();
}
holder2.text_title.setText(list.get(i).getCreatedAt());
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),holder2.img1);
break;
case 3:
ViewHolder3 holder3 = null;
if (view == null ){
view = View.inflate(context, R.layout.list3_layout, null);
holder3 = new ViewHolder3();
holder3.text_title = view.findViewById(R.id.title);
holder3.img1 = view.findViewById(R.id.img1);
holder3.img2 = view.findViewById(R.id.img2);
holder3.img3 = view.findViewById(R.id.img3);
view.setTag(holder3);
}else {
holder3 = (ViewHolder3) view.getTag();
}
holder3.text_title.setText(list.get(i).getCreatedAt());
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),holder3.img1);
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(1),holder3.img2);
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(2),holder3.img3);
break;
}
return view;
}
public class ViewHolde1{
TextView text_title;
}
public class ViewHolder2 extends ViewHolde1{
ImageView img1;
}
public class ViewHolder3 extends ViewHolder2{
ImageView img2;
ImageView img3;
}
}
import android.content.Context;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.bean.DataInfo;
import com.example.day8_lianxi2.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by 杨柳 on 2018/7/13.
*/
public class MyAdapter extends BaseAdapter{
private List<DataInfo.ResultsBean> list;
private Context context;
public MyAdapter(List<DataInfo.ResultsBean> list, Context context) {
this.list = list;
this.context = context;
}
//添加到集合元素0的前面
public void add(List<DataInfo.ResultsBean> resultsBeans){
this.list.addAll(0,resultsBeans);
notifyDataSetChanged();
}
//在集合后面添加元素
public void loadMore(List<DataInfo.ResultsBean> resultsBeans){
this.list.addAll(resultsBeans);
notifyDataSetChanged();
}
@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 getItemViewType(int position) {
List<String> images = list.get(position).getImages();
if (images == null || images.size() == 0){
return 0 ;
}else if (images.size()>2){
return 3;
}else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
int itemViewType = getItemViewType(i);
switch (itemViewType){
case 0:
ViewHolde1 holder1 = null;
if (view == null ){
view = View.inflate(context, R.layout.list1_layout, null);
holder1 = new ViewHolde1();
holder1.text_title = view.findViewById(R.id.title);
view.setTag(holder1);
}else {
holder1 = (ViewHolde1) view.getTag();
}
holder1.text_title.setText(list.get(i).getCreatedAt());
break;
case 1:
ViewHolder2 holder2 = null;
if (view == null ){
view = View.inflate(context, R.layout.list2_layout, null);
holder2 = new ViewHolder2();
holder2.text_title = view.findViewById(R.id.title);
holder2.img1 = view.findViewById(R.id.img1);
view.setTag(holder2);
}else {
holder2 = (ViewHolder2) view.getTag();
}
holder2.text_title.setText(list.get(i).getCreatedAt());
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),holder2.img1);
break;
case 3:
ViewHolder3 holder3 = null;
if (view == null ){
view = View.inflate(context, R.layout.list3_layout, null);
holder3 = new ViewHolder3();
holder3.text_title = view.findViewById(R.id.title);
holder3.img1 = view.findViewById(R.id.img1);
holder3.img2 = view.findViewById(R.id.img2);
holder3.img3 = view.findViewById(R.id.img3);
view.setTag(holder3);
}else {
holder3 = (ViewHolder3) view.getTag();
}
holder3.text_title.setText(list.get(i).getCreatedAt());
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),holder3.img1);
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(1),holder3.img2);
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(2),holder3.img3);
break;
}
return view;
}
public class ViewHolde1{
TextView text_title;
}
public class ViewHolder2 extends ViewHolde1{
ImageView img1;
}
public class ViewHolder3 extends ViewHolder2{
ImageView img2;
ImageView img3;
}
}