1.什么是多条目加载
* listView中展示的条目样式不相同的时候
2.应用场景
* 例如,今日头条展示的页面,条目展示的形式不同
3.如何使用多条目加载
* 1.public int getItemViewType(int position) {//返回条目的类型
* 2.public int getViewTypeCount() {//返回条目类型的数量
* 3.不同的ViewHolder,在优化之前先判断当前条目要展示哪种数据类型,然后在进行优化和数据的设置
难点:
listView 多条目展示 的难点在于,如何通过getItemViewType(int position) 获取不同类型数据?
前期 定义几个常量,方便在getView() 方法中直接获取getItemViewType(int position) 的类型
private static final int typeOne = 0;
一:首先在MyApp中:
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
}
二:在MyAcyivity:
@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:
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:
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;
}
}
三:Bean类
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
+ "]";
}
注意事项:
在build.gradle里面 要配置远程依赖
compile ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
* listView中展示的条目样式不相同的时候
2.应用场景
* 例如,今日头条展示的页面,条目展示的形式不同
3.如何使用多条目加载
* 1.public int getItemViewType(int position) {//返回条目的类型
* 2.public int getViewTypeCount() {//返回条目类型的数量
* 3.不同的ViewHolder,在优化之前先判断当前条目要展示哪种数据类型,然后在进行优化和数据的设置
难点:
listView 多条目展示 的难点在于,如何通过getItemViewType(int position) 获取不同类型数据?
前期 定义几个常量,方便在getView() 方法中直接获取getItemViewType(int position) 的类型
private static final int typeOne = 0;
一:首先在MyApp中:
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
}
二:在MyAcyivity:
@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:
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:
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;
}
}
三:Bean类
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
+ "]";
}
注意事项:
在build.gradle里面 要配置远程依赖
compile ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'