要想在一个ListView中使用多个布局文件,比如一个信息List包含了一个信息标题和每个信息对应的时间.
关键的步骤是实现Adapter类的getItemViewType 和getViewTypeCount 这两个方法
getItemViewType(int)
以int数值型返回itemView的类型。一般普通列表的item都是一样的布局,也就是说这个列表只有一种类型,但是很多时候我们需要列表显示不同的item,比如有的列表有普通item和separator两种类型,item用于响应用户点击事件,separator用于分隔item,不可以点击,这样这个列表就有了两种类型,重载这个方法,如果当前位置是item,我们可以返回1,如果是separator我们可以返回2,以此类推。重写getItemViewType方法,这个方法返回0到getViewTypeCount()-1之间的数字或者IGNORE_ITEM

getViewTypeCount()

返回这个Adapter将处理的itemView类型的总个数

1.首先定义一个接口,List中每一个Item项都必须实现它里面的getViewType和getView方法分别表示使用何种类型的View显示,以及如何新建和回收。

public interface Item {
public int getViewType();
public View getView(View convertView);
}

2.在Adapter中维护一个实现Item接口的List对象

public class MyListAdapter extends ArrayAdapter<Item> {
    private List<Item> items;
    private LayoutInflater inflater;
 
    public enum RowType {
        // Here we have two items types, you can have as many as you like though
        LIST_ITEM, HEADER_ITEM
    }
 
    public MyListAdapter(Context context, LayoutInflater inflater, List<Item> items) {
        super(context, 0, items);
        this.items = items;
        this.inflater = inflater;
    }
 
    @Override
    public int getViewTypeCount() {
        // Get the number of items in the enum
        return RowType.values().length;
 
    }
 
    @Override
    public int getItemViewType(int position) {
        // Use getViewType from the Item interface
        return items.get(position).getViewType();
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Use getView from the Item interface
        return items.get(position).getView(inflater, convertView);
    }
}

3.现在创建一个标题类实现Item接口

public class Header implements Item {
    private final String         name;
 
    public Header(String name) {
        this.name = name;
    }
 
    @Override
    public int getViewType() {
        return RowType.HEADER_ITEM.ordinal();
    }
 
    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            // No views to reuse, need to inflate a new view
            convertView = (View) inflater.inflate(R.layout.header, null);
        }
 
        TextView text = (TextView) convertView.findViewById(R.id.headerText);
        text.setText(name);
 
        return convertView;
    }
 
}

4.创建一个信息类实现Item接口

public class EventItem implements Item {
    private final String         str1;
    private final String         str2;
 
    public EventItem(String text1, String text2) {
        this.str1 = text1;
        this.str2 = text2;
    }
 
    @Override
    public int getViewType() {
        return RowType.LIST_ITEM.ordinal();
    }
 
    @Override
    public View getView(LayoutInflater inflater, View convertView) {
        if (convertView == null) {
            convertView = (View) inflater.inflate(R.layout.list_item, null);
        }
 
        TextView text1 = (TextView) convertView.findViewById(R.id.list_content1);
        TextView text2 = (TextView) convertView.findViewById(R.id.list_content2);
        text1.setText(str1);
        text2.setText(str2);
 
        return convertView;
    }
 
}

5.最后是Activity的实现

public class MainActivity extends ListActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        LayoutInflater inflater = LayoutInflater.from(this);
         
        List<Item> items = new ArrayList<Item>();
        items.add(new Header("Friday - November 30th 2012"));
        items.add(new EventItem("8:30am" , "Start work"));
        items.add(new EventItem("9:15am" , "Call Bob"));
        items.add(new EventItem("11:00am", "Meeting with Joe"));
        items.add(new EventItem("5:00pm" , "Freedom!"));
         
        items.add(new Header("Saturday - December 1st 2012"));
        items.add(new EventItem("8:30am" , "Keep sleeping"));
        items.add(new EventItem("10:00am", "Wake up"));
        items.add(new EventItem("11:00am", "Walk the dog"));
        items.add(new EventItem("6:00pm" , "Dinner at John's"));
         
        items.add(new Header("Sunday - December 2rd 2012"));
        items.add(new EventItem("8:30am" , "Keep sleeping"));
        items.add(new EventItem("10:00am", "Wake up"));
        items.add(new EventItem("11:00am", "Walk the dog"));
        items.add(new EventItem("6:00pm" , "Dinner at John's"));
 
        items.add(new Header("Monday - December 3rd 2012"));
        items.add(new EventItem("8:30am" , "Start work"));
        items.add(new EventItem("9:15am" , "Call Bob"));
        items.add(new EventItem("11:00am", "Meeting with Joe"));
        items.add(new EventItem("5:00pm" , "Freedom!"));
         
        MyListAdapter adapter = new MyListAdapter(this, inflater, items);
        setListAdapter(adapter);
    }
 
}

在ListView中使用多个布局的更多相关文章

  1. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  2. Android ListView中 每一项都有不同的布局

    实现代码 Adapter的代码 其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局 TYPE_1...是三种类型. 在使用不同布局的时候,getItemViewTyp ...

  3. Android开发-Listview中显示不同的视图布局

    1. 使用场景 在重写ListView的BaseAdapter时,我们常常在getView()方法中复用convertView,以提高性能.convertView在Item为单一的同种类型布局时,能够 ...

  4. ListView中嵌入布局的Button或多个点击事件

    有时候在ListView嵌入的布局中有多个事件需要点击,比如一个item中有TextView和Button两个布局,当我们需要获取这两个点击事件时,我们应该如何去获取呢,通常来说,我们都是已经固定好了 ...

  5. Android ListView中添加不同的多种布局

    最近做项目要使用ListView加载不同的布局,由于自己写的代码不能贴出,故找了一篇自认为比较好的blog给分享出来,希望对用到此项技术的同学有点帮助. http://logc.at/2011/10/ ...

  6. ListView中动态显示和隐藏Header&Footer

    ListView的模板写法 ListView模板写法的完整代码: android代码优化----ListView中自定义adapter的封装(ListView的模板写法) 以后每写一个ListView ...

  7. Android 如何在 ListView 中更新 ProgressBar 进度

    =======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...

  8. Android,LIstView中的OnItemClick点击无效的解决办法

    在List_Item布局文件中的根节点加上如下背景标黄的这一行 <?xml version="1.0" encoding="utf-8"?> < ...

  9. Android 实现ListView中Item被单击后背景色保持高亮

    今天为了解决一个需求,就是我有一个slidingDrawer,里面是一个ListView.然后,单击其中的Item,默认只是显示一个橙色背景后就恢复了.客户便有着个需求,需要单击这个Item的背景高亮 ...

随机推荐

  1. StyleCop 安装

    下载安装 官方网站:http://stylecop.codeplex.com/ 下载安装 如果自定义了安装路径请保证这个文件的AssemblyFile 属性正确的指向了StyleCop.dll所在的目 ...

  2. C# 操作数据库的几种方式(数据库使用SQL SERVER2008)

    一:通过常规 T-SQL 语句 (只写删除操作,其他同理) string strConn = ConfigurationManager.ConnectionStrings["SiteConn ...

  3. 如何搭建javaweb 开发环境

    一.准备工作: (1) 环境要求: 1.java jdk 2.eclipse--j2ee版 3.tomcat 4.mysql 5.HeidiSQL_7.0 (2)搭建步骤: 1.安装JDK,配置环境变 ...

  4. 在SQL Server 实现递归

    --在SQL Server 中其实提供了CTE[公共表表达式]来实现递归: Declare @Id Int Set @Id = 24;    ---在此修改父节点 With RootNodeCTE(I ...

  5. Android中一般支持的常用的距离单位

    px(像素):每个px对应屏幕上的一个点. dip或dp(device independent pixels,设备独立像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dip=1px. ...

  6. 100个iOS开发面试题汇总-王刚韧的技术博客

    100个iOS开发面试题汇总 关于iOS开发面试,不管对于招聘和应聘来说,面试都是很重要的一个环节,特别对于开发者来说,面试中的技术问题环节不仅是企业对应聘者技能和积累的考察,也是一个开发者自我检验的 ...

  7. c#安全性、稳定性、高性能需要注重的点

    1.弱事件 2.类型安全(包括类型转换时可能的异常) 3.线程安全 5.装箱和拆箱 6.闭包传递局部变量时可能遇到的问题. 7.用于dictionary的键,要同时实现GetHashCode和Equa ...

  8. JS数值输入控制

    在html文本框录入数值时,可用如下方法进行控制判断. 整数:<input type="text" name="aaa" onkeypress=" ...

  9. HDU 3555 Bomb (数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:从0开始到给定的数字N所有的数字中遇到“49”的数字的个数. Sample Input ...

  10. UVA 11078 Open Credit System(扫描 维护最大值)

    Open Credit System In an open credit system, the students can choose any course they like, but there ...