简单的多条目适配器代码 两种样式
public class MyAdapter extends BaseAdapter {
Context context; List<DataDataBean.DataBean> list;
int LEFT_ITEM=0;
int RIGHT_ITEM=1;
public MyAdapter(Context context, List<DataDataBean.DataBean> list) {
this.context = context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (position%2==0){
return LEFT_ITEM;
}
return RIGHT_ITEM;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (getItemViewType(i)==LEFT_ITEM){
ViewHolder viewholder;
if (view==null){
view = View.inflate(context,R.layout.xlistview_layout,null);
viewholder = new ViewHolder();
viewholder.imageview=view.findViewById(R.id.imageview);
viewholder.textview=view.findViewById(R.id.textview);
view.setTag(viewholder);
}else{
viewholder = (ViewHolder) view.getTag();
}
//viewholder.imageview.setImageResource(R.mipmap.ic_launcher);
ImageLoader.getInstance().displayImage(list.get(i).getImg(),viewholder.imageview, ImageLoderUtils.getDeFaultOption());
viewholder.textview.setText(list.get(i).getIntroduction());
}else if(getItemViewType(i)==RIGHT_ITEM){
ViewHolder viewholder;
if (view==null){
view = View.inflate(context,R.layout.right_layout,null);
viewholder = new ViewHolder();
viewholder.imageview=view.findViewById(R.id.imageview);
viewholder.textview=view.findViewById(R.id.textview);
view.setTag(viewholder);
}else{
viewholder = (ViewHolder) view.getTag();
}
//viewholder.imageview.setImageResource(R.mipmap.ic_launcher);
ImageLoader.getInstance().displayImage(list.get(i).getImg(),viewholder.imageview, ImageLoderUtils.getDeFaultOption());
viewholder.textview.setText(list.get(i).getIntroduction());
}
return view;
}
class ViewHolder{
ImageView imageview;
TextView textview;
}
}