简单的多条目适配器代码 ,:---------两种条目样式

简单的多条目适配器代码  两种样式


public class MyAdapter extends BaseAdapter {
    Context context; List<DataDataBean.DataBean> list;

    int LEFT_ITEM=0;
    int RIGHT_ITEM=1;

    public MyAdapter(Context context, List<DataDataBean.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;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position%2==0){
            return LEFT_ITEM;
        }
        return RIGHT_ITEM;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (getItemViewType(i)==LEFT_ITEM){
            ViewHolder viewholder;
            if (view==null){
                view = View.inflate(context,R.layout.xlistview_layout,null);
                viewholder = new ViewHolder();
                viewholder.imageview=view.findViewById(R.id.imageview);
                viewholder.textview=view.findViewById(R.id.textview);
                view.setTag(viewholder);
            }else{
                viewholder = (ViewHolder) view.getTag();
            }
            //viewholder.imageview.setImageResource(R.mipmap.ic_launcher);
            ImageLoader.getInstance().displayImage(list.get(i).getImg(),viewholder.imageview, ImageLoderUtils.getDeFaultOption());
            viewholder.textview.setText(list.get(i).getIntroduction());
        }else if(getItemViewType(i)==RIGHT_ITEM){
            ViewHolder viewholder;
            if (view==null){
                view = View.inflate(context,R.layout.right_layout,null);
                viewholder = new ViewHolder();
                viewholder.imageview=view.findViewById(R.id.imageview);
                viewholder.textview=view.findViewById(R.id.textview);
                view.setTag(viewholder);
            }else{
                viewholder = (ViewHolder) view.getTag();
            }
            //viewholder.imageview.setImageResource(R.mipmap.ic_launcher);
            ImageLoader.getInstance().displayImage(list.get(i).getImg(),viewholder.imageview, ImageLoderUtils.getDeFaultOption());
            viewholder.textview.setText(list.get(i).getIntroduction());
        }
        return view;
    }

    class ViewHolder{
        ImageView imageview;
        TextView textview;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值