实现的几个步骤如下:
一、显示效果图如下:
二、创建项目
1、项目构建图如下:
2、创建布局文件
2.1 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
2.2 first_item.xml
<?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="vertical" >
<TextView
android:id="@+id/first_tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
2.2 two_item.xml
<?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="50dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/two_tv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="aaaa"
android:textSize="20sp" />
<ImageView
android:id="@+id/two_ivv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/jx_left_listitem_1" />
</LinearLayout>
2.2 three_item.xml
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/three_tv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="aaaa"
android:textSize="20sp" />
<ImageView
android:id="@+id/three_iv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/jx_left_listitem_1" />
</LinearLayout>
3、src文件下
3.1 MainActivity.class
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
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.bwei.listView.bean.DataBean;
public class MainActivity extends Activity {
// 第一种
private static final int FirstType = 0;
private static final int TwoType = 1;
private static final int ThreeType = 2;
String[] texts = { "玉皇", "王母", "长蛾", "八戒", "如来", "凤姐" };
private int[] images = { R.drawable.jx_left_listitem_1,
R.drawable.jx_left_listitem_2, R.drawable.jx_left_listitem_3,
R.drawable.jx_left_listitem_4, R.drawable.jx_left_listitem_5,
R.drawable.jx_left_listitem_6 };
private List<DataBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
}
/**
* 初使化数据
*/
private void initData() {
list = new ArrayList<DataBean>();
int index1 = 0;
int index2 = 0;
int index3 = 0;
for (int i = 0; i < 10; i++) {
DataBean data = new DataBean();
// 第一种类型,认为就是只展示一个textView的
if (i % 2 == 0) {
data.setType(FirstType);
data.setText(texts[index1]);
index1 = (index1 + 1) % texts.length;
} else if (i % 3 == 0) {// 第二种类型,认为左边展示一个textView的,右边是一个图片的
data.setType(TwoType);
data.setText(texts[index2]);
data.setImages(images[index2]);
index2 = (index2 + 1) % texts.length;
} else {
// 第三种类型,认为上边展示一个textView的,下边是一个图片的
data.setType(ThreeType);
data.setText(texts[index3]);
data.setImages(images[index3]);
// 让索引值进行加加
index3 = (index3 + 1) % texts.length;
}
list.add(data);
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
/**
* 返回的是listView的类型
*/
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
/*Data d = data.get(position);
boolean has_image = d.has_image;
if (has_image == false) {
return 0;
} else if (has_image && d.image_list.size() == 3) {
return 2;
} else {
return 1;
}*/
}
/**
* item不同的条数
*/
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
ViewHolder3 holder3 = null;
// 拿到listView的不同的类型
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case FirstType:
holder1 = new ViewHolder1();
convertView = View.inflate(MainActivity.this,
R.layout.first_item, null);
holder1.tv1 = (TextView) convertView
.findViewById(R.id.first_tv);
convertView.setTag(holder1);
break;
case TwoType:
holder2 = new ViewHolder2();
convertView = View.inflate(MainActivity.this,
R.layout.two_item, null);
holder2.tv2 = (TextView) convertView
.findViewById(R.id.two_tv);
holder2.iv2 = (ImageView) convertView
.findViewById(R.id.two_ivv);
convertView.setTag(holder2);
break;
case ThreeType:
holder3 = new ViewHolder3();
convertView = View.inflate(MainActivity.this,
R.layout.three_item, null);
holder3.tv3 = (TextView) convertView
.findViewById(R.id.three_tv);
holder3.iv3 = (ImageView) convertView
.findViewById(R.id.three_iv);
convertView.setTag(holder3);
break;
}
} else {
switch (type) {
case FirstType:
holder1 = (ViewHolder1) convertView.getTag();
break;
case TwoType:
holder2 = (ViewHolder2) convertView.getTag();
break;
case ThreeType:
holder3 = (ViewHolder3) convertView.getTag();
break;
}
}
switch (type) {
case FirstType:
holder1.tv1.setText(list.get(position).getText());
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;
}
return convertView;
}
}
class ViewHolder1 {
TextView tv1;
}
class ViewHolder2 {
TextView tv2;
ImageView iv2;
}
class ViewHolder3 {
TextView tv3;
ImageView iv3;
}
}
3.1 DataBean.class
public class DataBean {
private String text;
private int images;
private int type;
public DataBean() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "DataBean [text=" + text + ", images=" + images + ", type="
+ type + "]";
}
public DataBean(String text, int images, int type) {
super();
this.text = text;
this.images = images;
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;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}