多条目

1.这是多条目的适配器

package adapter;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.example.w.huizong.R;
import com.nostra13.universalimageloader.core.ImageLoader;
 
import java.util.List;
 
import bean.AndroidBean;
 
public class MyAdapter extends BaseAdapter {
    private static final int ITEM_ONE =1 ;
    private static final int ITEM_TWO = 2;
    private static final int ITEM_THREE =3 ;
    private Context context;
    private List<AndroidBean.ResultsBean> resultsBeans;
    private viewHolder1 holder1;
 
    public MyAdapter(Context context, List<AndroidBean.ResultsBean> resultsBeans) {
        this.context = context;
        this.resultsBeans = resultsBeans;
    }
 
    @Override
    public int getCount() {
        return resultsBeans.size();
    }
 
    @Override
    public Object getItem(int position) {
        return resultsBeans.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public int getViewTypeCount() {
        return 4;
    }
 
    @Override
    public int getItemViewType(int position) {
        List<String> images = resultsBeans.get(position).getImages();
        if (images==null) {
            return ITEM_ONE;
        } else if (images.size() >2) {
            return ITEM_TWO;
        } else {
            return ITEM_THREE;
        }
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        switch (type) {
            case ITEM_ONE:
                viewHolder1 holder1=null;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_layout1, null);
                    holder1 = new viewHolder1();
                    holder1.desc = convertView.findViewById(R.id.text_desc);
                    holder1.time = convertView.findViewById(R.id.text_timer);
                    convertView.setTag(holder1);
                } else {
                    holder1= (viewHolder1) convertView.getTag();
                }
                holder1.time.setText(resultsBeans.get(position).getCreatedAt());
                holder1.desc.setText(resultsBeans.get(position).getDesc());
                break;
            case ITEM_TWO:
                viewHolder2 holder2=null;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_layout2, null);
                    holder2 = new viewHolder2();
                    holder2.desc = convertView.findViewById(R.id.text_desc1);
                    holder2.time = convertView.findViewById(R.id.text_timer1);
                    holder2.image1=convertView.findViewById(R.id.image_view1);
                    convertView.setTag(holder2);
                } else {
                    holder2= (viewHolder2) convertView.getTag();
                }
                holder2.time.setText(resultsBeans.get(position).getCreatedAt());
                try {
                    ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(0),holder2.image1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                holder2.desc.setText(resultsBeans.get(position).getDesc());
                break;
            case ITEM_THREE:
 
                viewHolder3 holder3=null;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_layout3, null);
                    holder3 = new viewHolder3();
                    holder3.desc = convertView.findViewById(R.id.text_desc3);
                    holder3.time = convertView.findViewById(R.id.text_timer3);
                    holder3.image1=convertView.findViewById(R.id.image_view2);
                    holder3.image2=convertView.findViewById(R.id.image_view3);
                    holder3.image3=convertView.findViewById(R.id.image_view4);
                    convertView.setTag(holder3);
                } else {
                    holder3= (viewHolder3) convertView.getTag();
                }
                holder3.time.setText(resultsBeans.get(position).getCreatedAt());
                try {
                    ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(0),holder3.image1);
                    ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(1),holder3.image2);
                    ImageLoader.getInstance().displayImage(resultsBeans.get(position).getImages().get(2),holder3.image3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                holder3.desc.setText(resultsBeans.get(position).getDesc());
                break;
        }
        return convertView;
    }
 
    class viewHolder{
        TextView desc;
        TextView time;
    }
    class viewHolder1 extends viewHolder{
 
    }
    class viewHolder2 extends viewHolder1{
        ImageView image1;
    }
    class viewHolder3 extends viewHolder2{
        ImageView image2;
        ImageView image3;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值