您的位置:首页 > Web前端 > CSS

ListView子布局样式有多个的处理

2015-12-15 13:57 501 查看
在开发中,ListView等类型的适配器控件的Item子布局很多时候都不是完全一样的,很可能有两个子布局样式,甚至更多的子布局样式,那么这种情况该怎么处理呢?

其实很简单,系统已经实现了加载多个子布局的方法,只需要在适配器中做相应处理即可。

在写ListView适配器时除了实现自动生成导入的必须重写的方法之外,还需要实现下面的两个方法:

《1》

public int getViewTypeCount() {}  返回子布局样式的个数。
《2》
public int getItemViewType(int position) {}   返回在ListView中position位置的Item呈现的子布局样式,当然这里的样式只是一个代号区分而已,具体样式是什么样的就应该在getView()方法里面返回对应样式的View即可。
《3》
public View getView(int position, View convertView, ViewGroup parent) {}
getView方法自然就与常规的不同了,需要去判断到底该返回什么样的子布局样式。
这里给个子布局有两种样式的适配器Demo例子:大家可以参考:
[java] view plaincopy
/*XlistView的适配器*/
public class XlistviewPlayAdapter extends BaseAdapter {
ArrayList<ContentBean> data = new ArrayList<ContentBean>() ;
private LayoutInflater inflater;
private Context context;
private final int VIEW_TYPE_COUNT = 2;  //子布局个数
private final int TYPE_1 = 0;   //子布局 1
private final int TYPE_2 = 1;   //
public XlistviewPlayAdapter(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}

public void refreshData(ArrayList<ContentBean> list){
this.data = list;  //这里变成添加数据
notifyDataSetChanged();
}

public void addData(ArrayList<ContentBean> list){
this.data.addAll(list);  //这里变成添加数据
notifyDataSetChanged();
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;   //子布局个数
}

@Override
public int getItemViewType(int position) {
if (data.get(position).getImgextra() == null){   //以数据源中的某个标签来判断加载那个子布局样式
return TYPE_1;
}else{
return TYPE_2;
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
Handler mHandler = null;
Type2_Handler type2_handler = null;
int view_type = getItemViewType(position); //得到对应位置子布局类型

if(convertView == null){   //逻辑判断
switch (view_type){
case TYPE_1:
mHandler = new Handler();
v = inflater.inflate(R.layout.xlistview_item_layout, null);
mHandler.iconImg = (ImageView) v.findViewById(R.id.xlistview_item_img);
mHandler.titleTxt = (TextView) v.findViewById(R.id.xlistview_item_title_txt);
mHandler.contentTxt = (TextView) v.findViewById(R.id.xlistview_content_text);
v.setTag(mHandler);
break;
case TYPE_2:
type2_handler = new Type2_Handler();
v = inflater.inflate(R.layout.xlistview_item_typetwo_layout,null);
type2_handler.title = (TextView) v.findViewById(R.id.xlistview2_item_title_txt);
type2_handler.oneImg = (ImageView) v.findViewById(R.id.img_one);
type2_handler.twoImg = (ImageView) v.findViewById(R.id.img_two);
type2_handler.threeImg = (ImageView) v.findViewById(R.id.img_three);
v.setTag(type2_handler);
break;
}
}else{
switch (view_type){
case TYPE_1:
v = convertView;
mHandler = (Handler) v.getTag();
break;
case TYPE_2:
v = convertView;
type2_handler = (Type2_Handler) v.getTag();
break;
}
}

ContentBean news = (ContentBean) getItem(position); //JAVABean对象
//设置数据
switch (view_type){
case TYPE_1:
mHandler.titleTxt.setText(news.getTitle());
mHandler.contentTxt.setText(news.getDigest());
//mHandler.iconImg
Picasso.with(context).load(news.getImgsrc()).into(mHandler.iconImg);
break;
case TYPE_2:
type2_handler.title.setText(news.getTitle());
ArrayList<ThreeprictureUrlBean> list = news.getImgextra();
Picasso.with(context).load(((ThreeprictureUrlBean)list.get(0)).getImgsrc())
.into(type2_handler.oneImg);
Picasso.with(context).load(((ThreeprictureUrlBean)list.get(1)).getImgsrc())
.into(type2_handler.twoImg);
Picasso.with(context).load(news.getImgsrc()).into(type2_handler.threeImg);
break;
}
return v;
}

//保存控件,有几个子布局就有多少个这样的类
class Handler{
ImageView iconImg;
TextView titleTxt;
TextView contentTxt;
}

class Type2_Handler{
TextView title;
ImageView oneImg,twoImg,threeImg;
}

}
两个样式的子布局:
1.只有一张图片加两个TextView
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="10dp">

<ImageView
android:id="@+id/xlistview_item_img"
android:layout_width="110dp"
android:layout_height="90dp"
android:background="@mipmap/base_common_default_icon_xsmall"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/xlistview_item_title_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@id/xlistview_item_img"
android:text="习近平将出席世界互联网大会"
android:textSize="18sp" />

<TextView
android:id="@+id/xlistview_content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/xlistview_item_title_txt"
android:layout_marginLeft="14dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/xlistview_item_img"
android:text="并发表主旨演讲:大会将在四川成都通惠门广场举行"
android:textSize="15sp" />

</RelativeLayout>
2.显示三张图片加一个TextView:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="10dp">

<TextView
android:id="@+id/xlistview2_item_title_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="习近平将出席世界互联网大会"
android:textSize="18sp" />

<LinearLayout
android:layout_below="@id/xlistview2_item_title_txt"
android:layout_width="match_parent"
android:layout_height="100dp">

<ImageView
android:id="@+id/img_one"
android:layout_marginRight="3dp"
style="@style/three_prcture" />
<ImageView
android:id="@+id/img_two"
android:layout_marginRight="3dp"
style="@style/three_prcture" />
<ImageView
android:id="@+id/img_three"
style="@style/three_prcture" />

</LinearLayout>

</RelativeLayout>

最后执行的就过就可以看到有不同的子布局呈现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: