Android StickyListHeaders实现电话本列表效果
程序员文章站
2023-12-02 19:02:16
本文实例为大家分享了stickylistheaders电话本列表效果的具体代码,供大家参考,具体内容如下
效果展示
布局文件
与listview使用方式一样,可以...
本文实例为大家分享了stickylistheaders电话本列表效果的具体代码,供大家参考,具体内容如下
效果展示
布局文件
与listview使用方式一样,可以在布局文件中直接进行使用
核心代码
oncreat方法
在oncreat方法中初始化控件,并为控件设置数据适配器
stickylistheaderslistview stickylist = (stickylistheaderslistview) findviewbyid(r.id.list); myadapter adapter = new myadapter(this); stickylist.setadapter(adapter);
设置数据适配器
在设置数据适配器的时候,需要先继承baseadapter,然后实现stickylistheadersadapter接口,最后实现里面为实现的方法
public class myadapter extends baseadapter implements stickylistheadersadapter { private string[] countries; private layoutinflater inflater; public myadapter(context context) { inflater = layoutinflater.from(context); countries = context.getresources().getstringarray(r.array.countries); } @override public int getcount() { return countries.length; } @override public object getitem(int position) { return countries[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.test_list_item_layout, parent, false); holder.text = (textview) convertview.findviewbyid(r.id.text); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text.settext(countries[position]); return convertview; } @override public view getheaderview(int position, view convertview, viewgroup parent) { headerviewholder holder; if (convertview == null) { holder = new headerviewholder(); convertview = inflater.inflate(r.layout.header, parent, false); holder.text = (textview) convertview.findviewbyid(r.id.text); convertview.settag(holder); } else { holder = (headerviewholder) convertview.gettag(); } //set header text as first char in name string headertext = + countries[position].subsequence(0, 1).charat(0); holder.text.settext(headertext); return convertview; } @override public long getheaderid(int position) { //return the first character of the country as id because this is what headers are based upon return countries[position].subsequence(0, 1).charat(0); } class headerviewholder { textview text; } class viewholder { textview text; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: jsp 页面显示的一些用法