import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by czx on 2017/8/17.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List
list;
public MyAdapter(Context context, List
list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list != null ? list.size():0;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//得到view布局类型
@Override
public int getItemViewType(int position) {
if(position%2 == 0){
return 0;
}else{
return 1;
}
}
//告诉适配器,要加载几种布局
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//得到view加载布局类型
int type = getItemViewType(position);
switch (type){
case 0:
ViewHolder viewHolder;
if(convertView == null){
convertView = View.inflate(context,R.layout.item1_layout,null);
viewHolder = new ViewHolder();
viewHolder.name1 = (TextView) convertView.findViewById(R.id.item1name1);
viewHolder.name2 = (TextView) convertView.findViewById(R.id.item1name2);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name1.setText(list.get(position).getName());
viewHolder.name2.setText(list.get(position).getArea());
break;
case 1:
ViewHolder viewHolder2;
if(convertView == null){
convertView = View.inflate(context,R.layout.item2_layout,null);
viewHolder2 = new ViewHolder();
viewHolder2.name1 = (TextView) convertView.findViewById(R.id.item2name1);
viewHolder2.name2 = (TextView) convertView.findViewById(R.id.item2name2);
convertView.setTag(viewHolder2);
}else{
viewHolder2 = (ViewHolder) convertView.getTag();
}
viewHolder2.name1.setText(list.get(position).getName());
viewHolder2.name2.setText(list.get(position).getArea());
break;
}
return convertView;
}
class ViewHolder{
TextView name1;
TextView name2;
}
class ViewHolder2{
TextView name1;
TextView name2;
}
}
多条目
最新推荐文章于 2019-01-04 10:27:54 发布