思路:
1 BaseAdapter 适配器
2 多个条目布局文件
3 适配器方法
在这里插入代码片
```public class MyAdapter extends BaseAdapter {
private Context context;
private List<JsonBean.DataBean> list;
public MyAdapter(Context context, List<JsonBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 3;
}
//这里是循环输出条目 排列就是 条目1 条目2 条目3 条目1 条目2 条目3···
@Override
public int getItemViewType(int position) {
return position % 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (getItemViewType(position)){
case 0:
MyHandler myHandler;
if(convertView == null){
myHandler = new MyHandler();
convertView = View.inflate(context,R.layout.item_gridview,null);
myHandler.textview = convertView.findViewById(R.id.textview);
myHandler.itme_image = convertView.findViewById(R.id.item_image);
convertView.setTag(myHandler);
}else{
myHandler = (MyHandler) convertView.getTag();
}
JsonBean.DataBean dataBean = list.get(position);
myHandler.textview.setText(dataBean.getNews_title());
ImageLoader.getInstance().displayImage(dataBean.getPic_url(),myHandler.itme_image);
break;
case 1:
MyHandler1 myHandler1;
if(convertView==null){
myHandler1 = new MyHandler1();
convertView = View.inflate(context,R.layout.item_2,null);
myHandler1.item_2_text1 = convertView.findViewById(R.id.item_2_text1);
myHandler1.item_2_text2 = convertView.findViewById(R.id.item_2_text2);
myHandler1.item_2_image = convertView.findViewById(R.id.item_2_image);
convertView.setTag(myHandler1);
}else{
myHandler1 = (MyHandler1) convertView.getTag();
}
JsonBean.DataBean dataBean1 = list.get(position);
myHandler1.item_2_text1.setText(dataBean1.getNews_title());
myHandler1.item_2_text2.setText(dataBean1.getNews_summary());
ImageLoader.getInstance().displayImage(dataBean1.getPic_url(),myHandler1.item_2_image);
break;
case 2:
MyHandler2 myHandler2;
if(convertView==null){
myHandler2 = new MyHandler2();
convertView = View.inflate(context,R.layout.item_3,null);
myHandler2.item_3_text = convertView.findViewById(R.id.item_3_text);
convertView.setTag(myHandler2);
}else{
myHandler2 = (MyHandler2) convertView.getTag();
}
JsonBean.DataBean dataBean2= list.get(position);
myHandler2.item_3_text.setText(dataBean2.getNews_title());
break;
}
return convertView;
}
class MyHandler{
TextView textview;
ImageView itme_image;
}
class MyHandler1{
TextView item_2_text1;
TextView item_2_text2;
ImageView item_2_image;
}class MyHandler2{
TextView item_3_text;
}
}