ImageLoader多级列表图片加载

//1.首先需要添加ImageLoader依赖搜索Image_Loader然后在注册清单里添加name切记添加MyApp类
public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ListView gv;
    public static final String JSON_URL = "http://fun.51fanli.com/api/taohuasuan/getHotItems/?c_src=5&cids=9000&page=1&size=10";
		
    Handler hander = new Handler(){
        public void handleMessage(android.os.Message msg) {
            showGoodsListView((String)msg.obj);//在gridview中展示商品
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn1);
        gv = (ListView) findViewById(R.id.lv);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    public void run() {
                        String rs = new NetWorkUtil().getJsonByGet(JSON_URL);
                        hander.sendMessage(hander.obtainMessage(2, rs));
                    };
                }.start();
            }
        });

    }

    public void showGoodsListView(String jsonData){
//		Toast.makeText(mcontext, jsonData, 1).show();
        Gson g = new Gson();
        GoodsMarket gm = g.fromJson(jsonData, GoodsMarket.class);
        ArrayList<Goods> listG = gm.getData().getItems();//得到商品的集合
        MyBaseAdapter1 adapter = new MyBaseAdapter1(MainActivity.this, listG);
        gv.setAdapter(adapter);

    }
}

//网络请求

public class NetWorkUtil {

	public String getJsonByGet(String jsonUrl){
		String data = "";
		try {
			URL url = null;
			HttpURLConnection urlConn = null;
			url = new URL(jsonUrl);
			urlConn = (HttpURLConnection) url.openConnection();//�õ�HttpsURLConnection��������
			urlConn.setConnectTimeout(5000);//�����������ʱ��
			urlConn.setReadTimeout(5000);//���ö�ȡʱ��
			int responseCode = urlConn.getResponseCode();//�õ���Ӧ��
			if(responseCode == 200){//200��ʾ��Ӧ�ɹ�
				InputStream inputStream = urlConn.getInputStream();//������Ӧ�����������
				byte[] buffer = new byte[1024];
				int length = 0;
				while((length=inputStream.read(buffer)) != -1){//˵�����û�ж�ȡ��
					String str = new String(buffer,0,length);//���ζ�ȡ�������
					data += str;//�ռ����ۼӣ����
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}

}
//ImageLoader

public class MyApp extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
    }
}
//适配器

public class MyBaseAdapter1 extends BaseAdapter{
	private Context mcontext;
	private List<Goods> list;

	
	public MyBaseAdapter1(Context mcontext, List<Goods> list) {
		super();
		this.mcontext = mcontext;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	控制有几种子布局
	 */
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	/**
	 * 获得当前数据要用到的布局
	 * @param position
	 * @return
     */
	@Override
	public int getItemViewType(int position) {
		if(position%2 == 0){//代表第一种布局
			return 0;
		}else{
			return 1;
		}

	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		ViewHolder1 holder1 = null;
		ViewHolder2 holder2 = null;
		int type = getItemViewType(position);//得到当前应该用的布局
		if(type == 0){//第一种布局
			if(convertView == null){
				holder1 = new ViewHolder1();
				convertView = LayoutInflater.from(mcontext).inflate(R.layout.goods_item_1line, null);
				holder1.img = (ImageView) convertView.findViewById(R.id.img);
				holder1.name = (TextView) convertView.findViewById(R.id.name);
				holder1.tg_price = (TextView) convertView.findViewById(R.id.tg_price);
				holder1.price = (TextView) convertView.findViewById(R.id.price);
				holder1.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
				convertView.setTag(holder1);
			}else{
				holder1 = (ViewHolder1) convertView.getTag();
			}
			//��ֵ
			Goods g = list.get(position);//ȡ����ǰ�е�����
			holder1.name.setText(g.getName());
			holder1.tg_price.setText("现价:"+g.getTg_price());
			holder1.price.setText("原价:"+g.getPrice());
			//给图片赋值
			ImageLoader.getInstance().displayImage(g.getImg3() ,holder1.img);

		}else{//第二种布局
			if(convertView == null){
				holder2 = new ViewHolder2();
				convertView = LayoutInflater.from(mcontext).inflate(R.layout.goods_item_1line2, null);
				holder2.name = (TextView) convertView.findViewById(R.id.name);
				holder2.tg_price = (TextView) convertView.findViewById(R.id.tg_price);
				holder2.price = (TextView) convertView.findViewById(R.id.price);
				holder2.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
				convertView.setTag(holder2);
			}else{
				holder2 = (ViewHolder2) convertView.getTag();
			}
			//��ֵ
			Goods g = list.get(position);//ȡ����ǰ�е�����
			holder2.name.setText(g.getName());
			holder2.tg_price.setText("现价:"+g.getTg_price());
			holder2.price.setText("原价:"+g.getPrice());
		}



		
		return convertView;
	}
	class ViewHolder1{
		ImageView img;
		TextView name;
		TextView tg_price;
		TextView price;
	}
	class ViewHolder2{
		TextView name;
		TextView tg_price;
		TextView price;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值