前言
以前我们总是用到listview显示数据,但是每个数据都长得差不多,但是实际需求却经常不是这样的,如果我要一个listview显示多种布局,那应该怎么办呢?
其实想要实现这样的功能并不难,关键就是在listview的适配器稍作修改,待会我会贴代码。我一般都是继承BaseAdapter这个来重写适配器,因为是父类,便于扩展。我们主要是重写里面的两个方法,getViewTypeCount()和getItemViewType,相信看这个方法名就应该知道是什么意思吧。
正文
这里我主要是实现了一个listview,前面十个数据是一种布局,后面十个数据是另一种布局。如果你想要更多的布局,那就随你咯,反正原理都一样。当然,你得建立两个item的布局,这里我就不贴上来了,没意义。
适配器代码如下:
public class TwoItemsAdapter extends BaseAdapter {
private Context context;
private List<ItemBeanOne> list1;//第一个布局的数据集
private List<ItemBeanTwo> list2;//第二个布局的数据集
private LayoutInflater inflater;
private final int type1=0;//第一种布局
private final int type2=1;//第二种布局
public TwoItemsAdapter(Context context, List<ItemBeanOne> list1, List<ItemBeanTwo> list2) {
this.context = context;
this.list1 = list1;
this.list2 = list2;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
int size= list1.size()+list2.size();
System.out.println("size"+size);
return size;
}
@Override
public Object getItem(int position) {
if(position<10){
return list1.get(position);
}else {
return list2.get(position);
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if(position<10){
return type1;
}else {
return type2;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolde1 holde1=null;
ViewHolde2 holde2=null;
int type=getItemViewType(position);
if(convertView==null){
switch (type){
case type1:
convertView=inflater.inflate(R.layout.item1, parent,false);
holde1=new ViewHolde1();
holde1.imageView= (ImageView) convertView.findViewById(R.id.id_img1);
holde1.title_tv= (TextView) convertView.findViewById(R.id.id_title_tv);
holde1.num_tv= (TextView) convertView.findViewById(R.id.id_num_tv);
ItemBeanOne beanOne=new ItemBeanOne();
beanOne=list1.get(position);
holde1.imageView.setImageResource(beanOne.getImageRes());
holde1.title_tv.setText(beanOne.getTitle());
holde1.num_tv.setText(beanOne.getNum());
convertView.setTag(holde1);
break;
case type2:
convertView=inflater.inflate(R.layout.item2,parent,false);
holde2=new ViewHolde2();
holde2.imageView= (ImageView) convertView.findViewById(R.id.id_img2);
holde2.one_tv= (TextView) convertView.findViewById(R.id.id_title2_tv);
holde2.two_tv= (TextView) convertView.findViewById(R.id.id_item2_two_tv);
holde2.three_tv= (TextView) convertView.findViewById(R.id.id_item2_three_tv);
holde2.four_tv= (TextView) convertView.findViewById(R.id.id_item2_four_tv);
holde2.logo_qq= (ImageView) convertView.findViewById(R.id.id_qq_img);
ItemBeanTwo two=new ItemBeanTwo();
two=list2.get(position-10);
holde2.imageView.setImageResource(two.getImgRes());
//这里是实现一个TextView显示不同颜色的字
String one_txt="<font color='black'>你是谁:</font>"+"<font color= 'blue'>"+two.getOne()+"</font>";
String two_txt="<font color='black'>我是谁:</font>"+"<font color= 'blue'>"+two.getTwo()+"</font>";
holde2.one_tv.setText(Html.fromHtml(one_txt));
holde2.two_tv.setText(Html.fromHtml(two_txt));
holde2.three_tv.setText(two.getThree());
holde2.four_tv.setText(two.getFour());
convertView.setTag(holde2);
}
}else {
switch (type){
case type1:
holde1= (ViewHolde1) convertView.getTag();
break;
case type2:
holde2= (ViewHolde2) convertView.getTag();
default:
break;
}
}
return convertView;
}
class ViewHolde1{
ImageView imageView;
TextView title_tv;
TextView num_tv;
}
class ViewHolde2{
ImageView imageView;
TextView one_tv;
TextView two_tv;
TextView three_tv;
TextView four_tv;
ImageView logo_qq;
}
}
调用适配代码:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<ItemBeanOne> ones=new ArrayList<ItemBeanOne>();//第一种数据
private List<ItemBeanTwo> twos=new ArrayList<ItemBeanTwo>();//第二种数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.id_listview);
initDatas();
TwoItemsAdapter adapter=new TwoItemsAdapter(MainActivity.this,ones,twos);
listView.setAdapter(adapter);
}
/**
* 初始化数据
*/
private void initDatas() {
for(int i=0;i<10;i++){
ItemBeanOne one=new ItemBeanOne();
one.setImageRes(R.drawable.img_good1);
one.setTitle("(标题:.." + i);
one.setNum("" + i * 100);
ones.add(one);
}
for(int j=10;j<20;j++){
ItemBeanTwo two=new ItemBeanTwo();
two.setImgRes(R.drawable.img_good1);
two.setOne("" + Math.random()*100);
two.setTwo(""+j);
two.setThree("无语:");
two.setFour("找工作:");
twos.add(two);
}
}
}
是不是很简单,就是这样,好了不多写了,投简历找工作去,太苦逼了。