Android ListView
listview的三种适配器
arrayadapter 支持list集合,只能展现一行文字
simpleadapter 具有扩展性,可以自定义多种效果
baseadapter 抽象类,继承这个类并且重写相关方法
常用属性
android:divider 设置分割线的颜色或图片
android:divider 分割线
android:scrollbars="none" 不显示滚动条
android:fadingedge="none" 去掉上边和下边黑色的阴影
android:fastscrollenable="true" 设置快速滚动
android:drawselectorontop="true" 设置长按试背景色高亮
android:stackfrombottom="true" 显示列表在最下面
android:cachecolorhint=“#000000” 设置背景时消除拖动的黑色阴影
android:headerdividersenabled=“false” 不会在页眉画分割线
android:footerdividersenabled=“false” 不会在页脚画分割线
android:descendantfocusability="blocksdescendants" 给子view设置,取消子控件的焦点
arrayadapter
1.系统自带布局
simple_list_item_1 : 单独一行的文本框,常用
simple_list_item_2 : 两个文本框组成
simple_list_item_checked : 每项都是由一个已选中的列表项 慎用
imple_list_item_multiple_choice : 都带有一个复选框 慎用
simple_list_item_single_choice : 都带有一个单选钮 慎用
setchoicemode(listview.choice_mode_multiple_modal) 设置多选状态 慎用
2.实例
listview.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_multiple_choice,new string[]{"元旦","清明","劳动节","端午节","中秋节"}));
simpleadapter
list<map<string, object>> mlist = new arraylist<map<string, object>>(); for (int i = 0; i < idcard.length; i++) { map<string, object> map = new hashmap<string, object>(); map.put("name", idcard[i]); map.put("sex", "sex"+i); map.put("number","number"+i); list.add(map); } simpleadapter adapter = new simpleadapter(this, mlist, r.layout.item_listview, new string[] { "name", "sex","number" },
newint[] {r.id.listview_item_name,r.id.listview_item_sex,r.id.listview_item_number });
自定义baseadapter模板
class myadapter extends baseadapter { private context context = null; public myadapter(context context) { this.context = context; } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder mholder; if (convertview == null) { mholder = new viewholder(); layoutinflater inflater = layoutinflater.from(context); convertview = inflater.inflate(r.layout.item_listview, null, true); mholder.text_item_listview_username = (textview) convertview.findviewbyid(r.id.listview_item_name); mholder.text_item_listview_email = (textview) convertview.findviewbyid(r.id.listview_item_sex); mholder.imageview_item_listview_headpic = (imageview) convertview.findviewbyid(r.id.listview_item_number); convertview.settag(mholder); } else { mholder = (viewholder) convertview.gettag(); } string name = list.get(position).get("name").tostring(); string sex = list.get(position).get("sex").tostring(); string number = list.get(position).get("number").tostring();
mholder.listview_item_name.settext(name); mholder.listview_item_sex.settext(sex); mholder.listview_item_number.settext(number); return convertview; } class viewholder { private textview listview_item_name; private textview listview_item_sex; private textview listview_item_number; } }
实现上拉刷新功能的listview
1.自定义adapter
public class mylistview extends listview implements onscrolllistener{ private footlayout flayout; private scroller scroller; private listviewlistener lvlistener; private float mlasty; private int itemconut=-1; private int pull_load_more_delta=150; private int scroll_duration=400; public mylistview(context context) { super(context); initview(context); // todo auto-generated constructor stub } public mylistview(context context, attributeset attrs) { super(context, attrs); initview(context); // todo auto-generated constructor stub } public mylistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initview(context); // todo auto-generated constructor stub } public void initview(context context){ super.setonscrolllistener(this); scroller=new scroller(context, new decelerateinterpolator()); flayout= new footlayout(context); flayout.setstate(footlayout.state_no); flayout.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub statrview(); } }); } @override public void setadapter(listadapter adapter) { // todo auto-generated method stub addfooterview(flayout); super.setadapter(adapter); } public void statrview(){ flayout.setstate(footlayout.state_loading); if(lvlistener!=null){ lvlistener.onloadmore(); } } public void stopview(){ flayout.setstate(footlayout.state_no); } @override public boolean ontouchevent(motionevent ev) { // todo auto-generated method stub switch (ev.getaction()) { case motionevent.action_down: mlasty=ev.getrawy(); break; case motionevent.action_move: final float deltay=ev.getrawy()-mlasty; mlasty=ev.getrawy(); if(getlastvisibleposition()==itemconut-1){ updatefooterheight(-deltay); } break; default: if (getlastvisibleposition() == itemconut - 1) { // 调用加载更多 if (flayout.getbottommargin() > pull_load_more_delta) { statrview();// 如果底部视图高度大于可以加载高度,那么就开始加载 } resetfooterheight();// 重置加载更多视图高度 } break; } return super.ontouchevent(ev); } private void updatefooterheight(float arg0){ int height=flayout.getbottommargin()+(int)arg0; if (height > pull_load_more_delta) { flayout.setstate(footlayout.state_ready); } else { flayout.setstate(footlayout.state_no); } flayout.setbottommargin(height); } private void resetfooterheight() { int bottommargin = flayout.getbottommargin(); if (bottommargin > 0) { scroller.startscroll(0, bottommargin, 0, -bottommargin, scroll_duration); invalidate(); } } @override public void computescroll() { if (scroller.computescrolloffset()) { flayout.setbottommargin(scroller.getcurry()); postinvalidate(); } super.computescroll(); } public interface listviewlistener { public void onloadmore(); } public void setlistviewlistener(listviewlistener l) { lvlistener=l; } @override public void onscroll(abslistview arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub itemconut=arg3; } @override public void onscrollstatechanged(abslistview arg0, int arg1) { // todo auto-generated method stub } }
2.底部footlayout
public class footlayout extends linearlayout{ static int state_ready=0,state_loading=1,state_no=2; private relativelayout rlayout; private textview tv; private progressbar pb; public footlayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub initview(context); } public footlayout(context context) { super(context); // todo auto-generated constructor stub initview(context); } public void initview(context context){ linearlayout layout=(linearlayout) layoutinflater.from(context).inflate(r.layout.foot_view, null); addview(layout); layout.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content)); rlayout=(relativelayout) layout.findviewbyid(r.id.foot_rlayout); tv=(textview) layout.findviewbyid(r.id.foot_text); pb=(progressbar) layout.findviewbyid(r.id.foot_progressbar); } public void hide(){ rlayout.setvisibility(view.visible); } public void show(){ rlayout.setvisibility(view.invisible); } public void setstate(int state){ tv.setvisibility(view.invisible); pb.setvisibility(view.invisible); if(state==state_ready){ tv.setvisibility(view.visible); tv.settext("松开显示更多"); }else if(state==state_loading){ pb.setvisibility(view.visible); }else{ tv.setvisibility(view.visible); tv.settext("加载更多"); } } public void setbottommargin(int height) { if (height < 0) return; linearlayout.layoutparams lp = (linearlayout.layoutparams) rlayout .getlayoutparams(); lp.bottommargin = height; rlayout.setlayoutparams(lp); } public int getbottommargin() { linearlayout.layoutparams lp = (linearlayout.layoutparams) rlayout .getlayoutparams(); return lp.bottommargin; } }
3.底部footlayout的布局
<?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" > <relativelayout android:id="@+id/foot_rlayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/foot_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载更多" android:layout_centerinparent="true" /> <progressbar android:id="@+id/foot_progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:verticalscrollbarposition="defaultposition" android:visibility="invisible" /> </relativelayout> </linearlayout>
4.使用mylistview
public class mainactivity extends activity implements ixlistviewlistener{ private listviews mylists; private simpleadapter madapter; private list<map<string,object>> mlist; private handler mhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mylists =(listviews) findviewbyid(r.id.mylistview); mlist=new arraylist<map<string,object>>(); madapter = new simpleadapter(mainactivity.this, getdata(), r.layout.list_item, new string[] { "name", "img", "content" }, new int[] { r.id.title, r.id.mimage, r.id.content }); mylists.setadapter(madapter); mylists.setpullloadenable(true); mylists.setxlistviewlistener(this); mhandler = new handler(); } string data[] = new string[] { "勇士", "火箭", "湖人", "凯尔特人", "雷霆" }; string data1[] = new string[] { "汤普森", "哈登", "詹姆斯", "塔图姆", "维斯布鲁克" }; private list<map<string, object>> getdata() { for (int i = 0; i < data.length; i++) { map<string, object> map = new hashmap<string, object>(); map.put("name", data[i]); map.put("content", data1[i]); map.put("img", r.drawable.ic_launcher); mlist.add(map); } return mlist; } // 加载更多 @override public void onloadmore() { mhandler.postdelayed(new runnable() { @override public void run() { getdata(); madapter.notifydatasetchanged(); onload(); } }, 1500); } private void onload() { mylists.stoploadmore(); } }
推荐阅读
-
Android自定义View 使用PathMeasure简单模仿系统ProgressBar(四)
-
Android studio中IDE窗口怎么显示或者隐藏?
-
Android studio怎么使用git获取最新内容然后合并?
-
Android Caused by: java.lang.ClassNotFoundException解决办法
-
Android开发之菜单(menu)用法实例分析
-
Android自定义ScrollView实现放大回弹效果实例代码
-
Android编程之ActionBar Tabs用法实例分析
-
Android自定义View圆形进度条控件(三)
-
Android studio中logcat提示信息设置?
-
Android获取本地相册图片和拍照获取图片的实现方法