1.使用场景
一个ListView中有多个布局的情况
2.ListView包含不同Item的布局
我们需要做这些工作:
1. 重写 getViewTypeCount() – 该方法返回多少个不同的布局
2. 重写 getItemViewType(int) – 根据position返回相应的Item
3. 根据view item的类型,在getView中创建正确的convertView
3.案例
package com.example.testing.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
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 java.util.ArrayList;
import javax.crypto.spec.PSource;
public class MainActivity extends AppCompatActivity {
private ListView listview;
private ArrayList<String> datas = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = ((ListView) findViewById(R.id.lv));
getdatas();
listview.setAdapter(new MyAdapter());
}
/**
* 获取数据
*
* @Author: ChengBin
* @Time: 16/3/6 上午11:17
*/
private void getdatas() {
for (int i = 0; i < 100; i++) {
datas.add(Integer.toString(i));
}
}
class MyAdapter extends BaseAdapter {
final int TYPE_1 = 0;
final int TYPE_2 = 1;
final int TYPE_3 = 2;
private LayoutInflater inflater;
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
int type = position % 3;
switch (type) {
case 0:
return TYPE_1;
case 1:
return TYPE_2;
case 2:
return TYPE_3;
}
return TYPE_1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 viewHolder1 = null;
ViewHolder2 viewHolder2 = null;
ViewHolder3 viewHolder3 = null;
inflater = LayoutInflater.from(MainActivity.this);
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_1:
convertView = inflater.inflate(R.layout.img, parent, false);
viewHolder1 = new ViewHolder1(convertView);
convertView.setTag(viewHolder1);
break;
case TYPE_2:
convertView = inflater.inflate(R.layout.text, parent, false);
viewHolder2 = new ViewHolder2(convertView);
convertView.setTag(viewHolder2);
break;
case TYPE_3:
convertView = inflater.inflate(R.layout.iv_tv, parent, false);
viewHolder3 = new ViewHolder3(convertView);
convertView.setTag(viewHolder3);
break;
}
} else {
switch (type) {
case TYPE_1:
viewHolder1 = ((ViewHolder1) convertView.getTag());
break;
case TYPE_2:
viewHolder2 = ((ViewHolder2) convertView.getTag());
break;
case TYPE_3:
viewHolder3 = ((ViewHolder3) convertView.getTag());
break;
}
}
switch (type) {
case TYPE_1:
break;
case TYPE_2:
viewHolder2.tv1.setText("item" + position);
break;
case TYPE_3:
viewHolder3.tv_2.setText("item" + position);
break;
}
return convertView;
}
}
class ViewHolder1 {
private ImageView iv1;
public ViewHolder1(View convertView) {
iv1 = ((ImageView) convertView.findViewById(R.id.iv_1));
}
}
class ViewHolder2 {
private TextView tv1;
public ViewHolder2(View convertView) {
tv1 = ((TextView) convertView.findViewById(R.id.tv_1));
}
}
class ViewHolder3 {
private ImageView iv_2;
private TextView tv_2;
public ViewHolder3(View convertView) {
iv_2 = ((ImageView) convertView.findViewById(R.id.iv_2));
tv_2 = ((TextView) convertView.findViewById(R.id.tv_2));
}
}
}