关于listview多条目显示item的方法

时间:2023-01-31 19:32:59

至于布局文件我就不写了,listview每一个不同item对应一个item的布局另外还有一个总listview布局
public class MainActivityextends AppCompatActivity {
//设置四种类型,对应我三周不同类型的item,数字随意生成,主要是为了区分;
private static final intFristTupe= 0;
private static final int TwoType = 1;
private static final int ThreeType= 2;
private static final int ForType=3;
private List<BaseAdap> list;
private ListView listview;

//设置数据
    private String[] texts = {"玉皇","王母","嫦娥","八戒","如来","悟空","XY", "xx", "gg", "pp"};
private int[] image1 = {R.drawable.ic_io, R.drawable.ic_01, R.drawable.ic_02, R.drawable.ic_03, R.drawable.ic_04, R.drawable.ic_05, R.drawable.ic_015, R.drawable.ic_018, R.drawable.ig_001, R.drawable.ig_002};
private int[] image2 = {R.drawable.ig_001, R.drawable.ig_002, R.drawable.ig_007, R.drawable.ic_018, R.drawable.ic_015,R.drawable.ic_05,R.drawable.ic_04, R.drawable.ic_01, R.drawable.ic_04, R.drawable.ic_02};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(newMyApdapter());
}

private void initData() {
list = new ArrayList<BaseAdap>();
for (intx = 0; x < 10; x++) {
BaseAdap data = new BaseAdap();
//第一种类型的数据,也就是4等等j,去展示一个类型
if(x % 4 == 0) {
data.setType(ForType);
data.setImages(image1[x]);
data.setImages2(image2[x]);
}//第二种类型的数据,也就是2等等j,去展示一个类型

else if (x % 2 == 0) {
data.setType(FristTupe);
data.setText(texts[x]);
System.out.println(texts[x] + x);
}//第三种类型的数据
else if(x % 3 == 0) {
data.setType(TwoType);
data.setText(texts[x]);
data.setImages(image1[x]);
}//第四种类型的数据
else{
data.setType(ThreeType);
data.setText(texts[x]);
data.setImages(image1[x]);
System.out.println(texts[x] + x);
}
list.add(data);
}
}

//ListView多条目的Adapter,他比我们普通写的Adapter多了两个方法
private classMyApdapter extends BaseAdapter {
class ViewHolder1 {
TextView tv1;
}

class ViewHolder2 {
TextView tv2;
ImageView iv2;
}

class ViewHolder3 {
TextView tv3;
ImageView iv3;
}
class ViewHolder4 {
ImageView iv4,tv4;

}

@Override
public intgetCount() {
return list.size();
}

//返回ListView所加载Item的类型
@Override
public intgetItemViewType(int position) {
//得到databean里面的类型数据
returnlist.get(position).getType();
}

//你的ListView有几种类型的Item,我们Demo一共有四种不同的类型
@Override
public intgetViewTypeCount() {
return 4;
}

/*
1、创建ViewHodler
2、复用参数
3、查找控件
4、给控件设置数据
*/
@Override
publicView getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
ViewHolder3 holder3 = null;
ViewHolder4 holder4 = null;
//拿到ListView当前Item所对应的DataBean数据的Type数据
inttype = getItemViewType(position);
//存入
if(convertView == null) {
//更加类型创建ViewHolder
switch(type) {
case FristTupe:
holder1 = new ViewHolder1();
convertView = View.inflate(MainActivity.this, R.layout.item,null);
holder1.tv1 = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder1);
break;
case TwoType:
holder2 = new ViewHolder2();
convertView = View.inflate(MainActivity.this, R.layout.item2,null);
holder2.tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder2.iv2 = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder2);
break;
case ThreeType:
holder3 = new ViewHolder3();
convertView = View.inflate(MainActivity.this, R.layout.item3,null);
holder3.tv3 = (TextView) convertView.findViewById(R.id.textView3);
holder3.iv3 = (ImageView) convertView.findViewById(R.id.imageView2);
convertView.setTag(holder3);
break;
case ForType:
holder4 = new ViewHolder4();
convertView = View.inflate(MainActivity.this, R.layout.item4,null);
holder4.tv4 = (ImageView) convertView.findViewById(R.id.imageView3);
holder4.iv4 = (ImageView) convertView.findViewById(R.id.imageView4);
convertView.setTag(holder4);
break;
}
//取东西
}else {
switch (type) {
case FristTupe:
holder1 = (ViewHolder1) convertView.getTag();
break;
case TwoType:
holder2 = (ViewHolder2) convertView.getTag();
break;
case ThreeType:
holder3 = (ViewHolder3) convertView.getTag();
break;
case ForType:
holder4 = (ViewHolder4) convertView.getTag();
break;
}
}
//设置数据
switch(type) {
case FristTupe:
BaseAdap dataBean = list.get(position);
String text = dataBean.getText();
holder1.tv1.setText(text);
break;
case TwoType:
holder2.tv2.setText(list.get(position).getText());
holder2.iv2.setImageResource(list.get(position).getImages());
break;
case ThreeType:
holder3.tv3.setText(list.get(position).getText());
holder3.iv3.setImageResource(list.get(position).getImages());
break;
case ForType:
holder4.tv4.setImageResource(list.get(position).getImages2());
holder4.iv4.setImageResource(list.get(position).getImages());
break;
}
return convertView;
}
@Override
publicObject getItem(int i) {
return null;
}
@Override
public longgetItemId(int i) {
return 0;
}
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class BaseAdap {
//设置类型
private int type;
//文本数据
private String text;
//图片数据
private int images;
private int images2;

public int getImages2() {
return images2;
}

public void setImages2(int images2) {
this.images2 = images2;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public int getImages() {
return images;
}

public void setImages(int images) {
this.images = images;
}