ListView多条目+ImageLoader

权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

写一个类继承Application

package com.wzq.moreitemlistviewdemo;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class MApp extends Application {
//先注册.App
@Override
public void onCreate() {
    super.onCreate();
    //开始构建
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
    //初始化imageloader;
    ImageLoader.getInstance().init(config);

    }
}

封装好的Bean类

package com.wzq.moreitemlistviewdemo;

public class Goods {
private String name;
private String price;
private int resId;
private int typeId;

public Goods(String name, String price, Integer typeId) {
    super();
    this.name = name;
    this.price = price;
    this.typeId = typeId;
}

public Goods(String name, String price, int resId, Integer typeId) {
    super();
    this.name = name;
    this.price = price;
    this.resId = resId;
    this.typeId = typeId;
}

public int getTypeId() {
    return typeId;
}
public void setTypeId(int typeId) {
    this.typeId = typeId;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPrice() {
    return price;
}
public void setPrice(String price) {
    this.price = price;
}
public int getResId() {
    return resId;
}
public void setResId(int resId) {
    this.resId = resId;
}
@Override
public String toString() {
    return "Goods [name=" + name + ", price=" + price + ", resId=" + resId
            + "]";
    }
}

主界面

package com.wzq.moreitemlistviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

@BindView(R.id.lv)
ListView lv;
private ArrayList<Goods> goodsList;
String imgUrl = "http://image.tianjimedia.com/uploadImages/2012/067/N80N0GUA36N0.jpg";
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //使用butterKnife来注解控件;
    ButterKnife.bind(this);

    initData();
}

private void initData() {
    goodsList = new ArrayList<Goods>();
    goodsList.add(new Goods("商品1", "21", 0));
    goodsList.add(new Goods("商品3", "23", 0));
    goodsList.add(new Goods("商品4", "24", R.drawable.hu, 1));
    goodsList.add(new Goods("商品2", "22", R.drawable.hu, 1));
    goodsList.add(new Goods("商品5", "25", 0));
    goodsList.add(new Goods("商品6", "26", R.drawable.hu, 1));
    goodsList.add(new Goods("商品21", "21", 0));
    goodsList.add(new Goods("商品5", "25", 0));
    goodsList.add(new Goods("商品22", "22", R.drawable.hu, 1));
    goodsList.add(new Goods("商品23", "23", 0));
    goodsList.add(new Goods("商品24", "24", R.drawable.hu, 1));
    goodsList.add(new Goods("商品25", "25", 0));
    goodsList.add(new Goods("商品26", "26", R.drawable.hu, 1));
    goodsList.add(new Goods("商品31", "21", 0));
    goodsList.add(new Goods("商品32", "22", R.drawable.hu, 1));
    goodsList.add(new Goods("商品34", "24", R.drawable.hu, 1));
    goodsList.add(new Goods("商品36", "26", R.drawable.hu, 1));
    goodsList.add(new Goods("商品33", "23", 0));
    goodsList.add(new Goods("商品35", "25", 0));

    //把数据配置给listview
    lv.setAdapter(new MBaseAdapter());
}

class MBaseAdapter extends BaseAdapter {
    //返回条目的类型
    private final int type1 = 0;
    private final int type2 = 1;

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int i) {
        return goodsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        //拿到类型
        int typeId = goodsList.get(i).getTypeId();
        switch (typeId) {
            case type1://0
                Holder1 holder1 = null;
                if (convertView == null) {
                    holder1 = new Holder1();
                    convertView = View.inflate(MainActivity.this, R.layout.lv_item_1, null);
                    holder1.textView1 = convertView.findViewById(R.id.textView1);
                    holder1.textView2 = convertView.findViewById(R.id.textView2);
                    convertView.setTag(holder1);
                } else {
                    holder1 = (Holder1) convertView.getTag();
                }
                holder1.textView1.setText(goodsList.get(i).getName());
                holder1.textView2.setText(goodsList.get(i).getPrice());
                break;

            case type2://1
                Holder2 holder2 = null;
                if (convertView == null) {
                    holder2 = new Holder2();
                    convertView = View.inflate(MainActivity.this, R.layout.lv_item_2, null);
                    holder2.textView1 = convertView.findViewById(R.id.textView1);
                    holder2.textView2 = convertView.findViewById(R.id.textView2);
                    holder2.imageView1 = convertView.findViewById(R.id.imageView1);
                    convertView.setTag(holder2);
                } else {
                    holder2 = (Holder2) convertView.getTag();
                }
                holder2.textView1.setText(goodsList.get(i).getName());
                holder2.textView2.setText(goodsList.get(i).getPrice());

                    //holder2.imageView1.setImageResource(R.drawable.hu);
                ImageLoader.getInstance().displayImage(imgUrl, holder2.imageView1);

                break;
        }
        return convertView;
    }

    //返回条目有多少种类型
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    //返回条目的类型
    @Override
    public int getItemViewType(int position) {
        return goodsList.get(position).getTypeId();//返回类型
    }
}

class Holder1 {
    TextView textView1, textView2;
}

class Holder2 {
    TextView textView1, textView2;
    ImageView imageView1;
    }

}

主页面布局

<ListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

两个item布局

item1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="TextView"
    android:textSize="24sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="TextView"
    android:textSize="24sp" />

</LinearLayout>

item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="TextView"
    android:textSize="24sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="TextView"
    android:textSize="24sp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值