Listview中加载多种布局

来源:互联网 发布:尼康镜头大三元知乎 编辑:程序博客网 时间:2024/06/04 17:48

在listview上填充布局时,很多listview上的布局并不是单一的,很多listview上有不同的布局,要实现这种效果要在listview上的适配器中设置。一般情况下,我们只需重写adapter中的4个方法即可(getCount,getItem,getItemId,getView),如果要实现多种布局还需要重写getItemViewType,getViewTypeCount这两个方法。

@Overridepublic int getItemViewType(int position) {//这个方法就是要告诉adapt要加载哪个布局,而且这个方法和getview类似,会多次执行。    img_path=list.get(position).get("thumbnail").toString();    if (img_path.equals("")){        return 0;//这个返回值就相当于一个标记,在getview()方法中可以利用getItemViewType(potion)得到这个标记    }else{        return 1;    }}@Overridepublic int getViewTypeCount() {    return 2;//加载多少个布局,加载两个布局,就返回2}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    MyView myView = null;//不同布局对于的内部类    MyView1 myView1=null;    int type = getItemViewType(position);//得到标记    if (convertView==null){//根据得到的标记,加载不同的布局







0 0
原创粉丝点击