ListView多条目+ImageLoader
来源:互联网 发布:常见网络诈骗手段有 编辑:程序博客网 时间:2024/05/16 06:59
权限
<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@Overridepublic 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;}@Overridepublic 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;@Overrideprotected 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>
- ListView多条目+ImageLoader
- ImageLoader+网络请求+ListView多条目加载
- ListView多条目加载和ImageLoader
- ImageLoader多张图片&listview多条目加载
- ImageLoader+网络请求+List多条目展示
- ListView多条目展示
- ListView多条目显示
- ListView多条目展示
- listview多条目展示
- ListView多条目加载
- listView 多条目展示
- listView多条目显示
- ListView 多条目加载
- listView多条目
- ListView多条目展示
- listview多条目展示
- ListView多条目展示
- listview多条目加载
- 正在找工作,回顾十几年工作中的一些小事
- 免费提供一台已搭建好的 nginx + FastDFS图片服务器(Centos6.4),并已测试上传功能
- Apache服务器安装
- Service Cloud関連
- css样式基础
- ListView多条目+ImageLoader
- 厦门亿联2018面试题
- BZOJ 1800 AHOI 2009 fly 飞行棋
- webstrom 配置 es6 的运行环境
- jqGrid禁止某条记录被选中
- 密码编码学初探——分组加密技术(DES&AES)
- java源文件当中可以有多个类,但是为什么只能有一个public类呢?而当这个类被修饰为public的话,为什么源文件名必须要与类名相同呢?
- Android中Application类用法
- android Drawable总结