您的位置:首页 > 其它

ListView多条目加载

2016-08-22 21:03 323 查看
适配器:

package com.ex.moni;

import java.util.List;

import com.bawei.bean.Data;
import com.bawei.bean.Image_listE;
import com.bawei.bean.Large_image_list;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
private List<Data> list;
private Context context;
private ImageLoader imageLoader;

public MyAdapter(List<Data> list, Context context) {
super();
this.list = list;
this.context = context;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
viewhoder1 view1 = null;
viewhode2 view2 = null;
viewhoder3 view3 = null;
viewhoder4 view4 = null;
if (convertView == null) {
switch (type) {
// 一张小图
case 0:
view1 = new viewhoder1();
convertView = View.inflate(context, R.layout.item0, null);
view1.tve = (TextView) convertView
.findViewById(R.id.title_ite0);
view1.iView = (ImageView) convertView
.findViewById(R.id.imageView_item0);
view1.tvname = (TextView) convertView
.findViewById(R.id.title_itename);
view1.tvpl = (TextView) convertView.findViewById(R.id.title_pl);
convertView.setTag(view1);

break;
// 三张小图
case 1:
view2 = new viewhode2();
convertView = View.inflate(context, R.layout.item1, null);
view2.tve2 = (TextView) convertView
.findViewById(R.id.title_item1);
view2.tvname = (TextView) convertView
.findViewById(R.id.item1_tevname);
view2.tepl = (TextView) convertView
.findViewById(R.id.item1_tepl);
view2.iv1 = (ImageView) convertView
.findViewById(R.id.imageView_item1);
view2.iv2 = (ImageView) convertView
.findViewById(R.id.imageView_item1_2);
view2.iv3 = (ImageView) convertView
.findViewById(R.id.imageView_item1_3);
convertView.setTag(view2);

break;
// 一张大图
case 2:
view3 = new viewhoder3();
convertView = View.inflate(context, R.layout.item2, null);

view3.tve3 = (TextView) convertView
.findViewById(R.id.title_item2);
view3.tvname = (TextView) convertView
.findViewById(R.id.item2_tvname);
view3.tvpl = (TextView) convertView
.findViewById(R.id.item2_tvepl);
view3.ivda1 = (ImageView) convertView
.findViewById(R.id.imageView_item2);
convertView.setTag(view3);

break;
// 没有图
case 3:
view4 = new viewhoder4();
convertView = View.inflate(context, R.layout.item3, null);
view4.tve4 = (TextView) convertView
.findViewById(R.id.title_item3);
view4.tvname = (TextView) convertView
.findViewById(R.id.title_item3_1);
view4.tvpl = (TextView) convertView
.findViewById(R.id.title_item3_3);
convertView.setTag(view4);

break;

}
} else {
switch (type) {
case 0:
view1 = (viewhoder1) convertView.getTag();
break;
case 1:
view2 = (viewhode2) convertView.getTag();
break;
case 2:
view3 = (viewhoder3) convertView.getTag();
break;
case 3:
view4 = (viewhoder4) convertView.getTag();
break;

}

}
// 赋值
switch (type) {
case 0:
view1.tve.setText(list.get(position).getTitle());
view1.tvpl.setText("评论 " + list.get(position).getTitle());
view1.tvname.setText(list.get(position).getTitle());
imageLoader.displayImage(list.get(position).getImage_list().get(0)
.getUrl(), view1.iView);
break;

case 1:
view2.tve2.setText(list.get(position).getTitle());
view2.tvname.setText(list.get(position).getTitle());
view2.tepl.setText("评论 " + list.get(position).getTitle());
imageLoader.displayImage(list.get(position).getImage_list().get(0)
.getUrl(), view2.iv1);
imageLoader.displayImage(list.get(position).getImage_list().get(1)
.getUrl(), view2.iv2);
imageLoader.displayImage(list.get(position).getImage_list().get(2)
.getUrl(), view2.iv3);
break;
case 2:
view3.tve3.setText(list.get(position).getTitle());
view3.tvname.setText(list.get(position).getTitle());
view3.tvpl.setText("评论 " + list.get(position).getTitle());
imageLoader.displayImage(list.get(position).getLarge_image_list()
.get(0).getUrl(), view3.ivda1);
break;
case 3:
view4.tve4.setText(list.get(position).getTitle());
view4.tvname.setText(list.get(position).getTitle());
view4.tvpl.setText("评论 " + list.get(position).getTitle());
break;

}
return convertView;
}

@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
Data bean = list.get(position);
List<Image_listE> list2 = bean.getImage_list();
List<Large_image_list> list3 = bean.getLarge_image_list();
if (list2 != null && list2.size() == 1) {
// 一张小图
return 0;
} else if (list2 != null && list2.size() == 3) {
// 三张小图
return 1;
// 一张大图
} else if (list3 != null && list3.size() == 1) {
return 2;
} else {
return 3;
}

}

@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 4;
}

// 一张小图
class viewhoder1 {
TextView tve, tvname, tvpl;
ImageView iView;

}

// 三张小图
class viewhode2 {
TextView tve2, tvname, tepl;
ImageView iv1;
ImageView iv2;
ImageView iv3;
}

// 一张大图
class viewhoder3 {
TextView tve3, tvname, tvpl;
ImageView ivda1;
}

class viewhoder4 {
TextView tve4, tvname, tvpl;
}

}


布局0 一张小图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RelativeLayout
android:id="@+id/rel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" >

<TextView
android:id="@+id/title_ite0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm" />

<TextView
android:id="@+id/title_itename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="来源"
android:textColor="#B5B5B5" />

<TextView
android:id="@+id/title_pl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="90dp"
android:text="评论"
android:textColor="#B5B5B5" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/rr"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >

<ImageView
android:id="@+id/imageView_item0"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>

</LinearLayout>


布局1 三张小图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/title_item1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageView_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView_item1_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView_item1_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/item1_tevname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="通道大叔"
android:textColor="#B5B5B5" />

<TextView
android:id="@+id/item1_tepl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="评论"
android:textColor="#B5B5B5" />
</LinearLayout>

</LinearLayout>


布局2 一张大图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/title_item2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imageView_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/item2_tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新民"
android:textColor="#B5B5B5" />

<TextView
android:id="@+id/item2_tvepl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="pl"
android:textColor="#B5B5B5" />
</LinearLayout>

</LinearLayout>


布局3 没有图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/title_item3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="mm" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/title_item3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zz"
android:textColor="#B5B5B5" />

<TextView
android:id="@+id/title_item3_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="评论"
android:textColor="#B5B5B5" />
</LinearLayout>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: