在android 开发中使用listview是很常见的,我们常常使用的是加载一种布局,当需要几种不同的布局的时候,开始的时候都是用Visiable 和 Gone 来显示和隐藏来展示。
其实android 也提供了一个api 就是继承BaseAdapter的时候 多重写2个方法:
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
用来记录不同的view类型,getItemViewType 方法返回值最好是从0开始。 getViewTypeCount 返回值 是不能布局的个数。
话不多说看代码:
我这里加载了三种布局,数据50条;
//数据实体
public class Data implements Serializable {
private static final long serialVersionUID = 1L;
public int type;
public String Text;
public Bitmap icon;
}
布局:acivity_main.xml
<LinearLayout 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"
android:orientation="vertical"
>
<ListView android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
/>
</LinearLayout>
item01.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/tv_item01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一种独立的布局1"/>
</LinearLayout>
item02.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="horizontal" >
<ImageView android:id="@+id/iv_img02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<TextView
android:id="@+id/tv_item02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一种独立的布局2"/>
</LinearLayout>
item03.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="horizontal" >
<TextView
android:id="@+id/tv_item03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一种独立的布局3"/>
<ImageView android:id="@+id/iv_img01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
MyAdater.java
class MyAdapter extends BaseAdapter{
LayoutInflater mInflater;
private List<Data> datas;
private Context context;
public MyAdapter(Context context,List<Data> datas) {
//mInflater = LayoutInflater.from(context);
this.datas = datas;
Log.e("peng", "adapter size:"+datas.size());
this.context = context;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 返回那种类型
*/
@Override
public int getItemViewType(int position) {
Data data = datas.get(position);
return data.type;
}
/**
* 有几种view类型
*/
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder_text holder1 = null;
Holderimg_text holder2 = null ;
Holder_img holder3 = null;
int type = getItemViewType(position);
if(convertView == null){
if(type ==1){
Log.e("peng", "1");
holder1 = new Holder_text();
convertView = View.inflate(context,R.layout.item01, null);
holder1.text = (TextView) convertView.findViewById(R.id.tv_item01);
convertView.setTag(holder1);
}else if(type == 2) {
Log.e("peng", "2");
holder2 = new Holderimg_text();
convertView = View.inflate(context,R.layout.item02, null);
holder2.text = (TextView) convertView.findViewById(R.id.tv_item02);
holder2.img = (ImageView) convertView.findViewById(R.id.iv_img02);
convertView.setTag(holder2);
}else{
Log.e("peng", "3");
holder3 = new Holder_img();
convertView = View.inflate(context,R.layout.item03, null);
holder3.img = (ImageView) convertView.findViewById(R.id.iv_img01);
holder3.text = (TextView) convertView.findViewById(R.id.tv_item03);
convertView.setTag(holder3);
}
}else {
if(type == 1){
holder1 = (Holder_text) convertView.getTag();
holder1.text.setText(datas.get(position).Text+"type:"+datas.get(position).type);
}else if(type == 2){
holder2 = (Holderimg_text) convertView.getTag();
holder2.text.setText(datas.get(position).Text+"type:"+datas.get(position).type);
}else {
holder3 = (Holder_img) convertView.getTag();
holder3.text.setText(datas.get(position).Text+"type:"+datas.get(position).type);
}
}
return convertView;
}
public final class Holderimg_text{
public TextView text;
public ImageView img;
}
public final class Holder_text{
public TextView text;
}
public final class Holder_img{
public ImageView img;
public TextView text;
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
}
MainActivity.java
public class MainActivity extends Activity {
private ListView lv_list;
private MyAdapter adapter;
private List<Data>list = new ArrayList<Data>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data();
Log.e("peng","size"+list.size());
initView();
}
private void data() {
for(int i = 0;i<50;i++){
Data d = new Data();
if((i%3)==0){
d.type = 0;
}else if((i%3)==1){
d.type = 1;
}else{
d.type = 2;
}
d.Text = "这是第"+i+"个";
list.add(d);
}
}
private void initView() {
lv_list = (ListView) findViewById(R.id.lv_list);
adapter = new MyAdapter(MainActivity.this,list);
lv_list.setAdapter(adapter);
}
}
这样 就完成了ListView加载不同的三种布局。