Android ListView万能适配器实例代码
listview是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义。
最近参考一些资料,发现一个万能listview适配器,代码量少,节省时间,总结一下分享给大家。
首先有一个自定义的adapter继承于baseadapter,下面是自定义的adapter,精华在getview()方法中
package com.example.mylistview.util; import java.util.list; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; public abstract class commonadapter<t> extends baseadapter { /** * 上下文 */ private context mcontext; /** * 实体类集合 */ private list<t> mdatas; private layoutinflater minflater; /** * 控件id */ private int mlayoutid; public commonadapter(context context, list<t> datas, int layoutid) { this.mcontext = context; this.mdatas = datas; this.mlayoutid = layoutid; minflater = layoutinflater.from(context); } @override public int getcount() { // todo auto-generated method stub return mdatas.size(); } @override public t getitem(int arg0) { // todo auto-generated method stub return mdatas.get(arg0); } @override public long getitemid(int arg0) { // todo auto-generated method stub return arg0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { // todo auto-generated method stub viewholder holder = viewholder.get(mcontext, arg1, arg2, mlayoutid, arg0); convert(holder, getitem(arg0)); return holder.getconvertview(); } public abstract void convert(viewholder holder, t t); }
以上的抽象方法convert(viewholder holder, t t);就相当于以前通用代码中的
viewholder.mtextview = (textview) convertview .findviewbyid(r.id.id_tv_title);
viewholder.mtextview.settext(bean.getname());
找到控件的id再去设施settext等重复的代码方法中的参数viewholder holder, t t holder就相当于以前通用代码中的viewholder,t就相当于一个自己定义的实体类bean。
以上代码中getview()方法中有一个viewholder是需要自己声明的,以下是代码以及详细注释:
package com.example.mylistview.util; import android.content.context; import android.graphics.bitmap; import android.renderscript.type; import android.util.sparsearray; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; public class viewholder { /** * sparsearray类存放view集合 */ private sparsearray<view> mviews; /** * */ private int mposition; /** * 布局文件 */ private view mconvertview; public view getconvertview() { return mconvertview; } public viewholder(context context, viewgroup parent, int layoutid, int position) { this.mviews = new sparsearray<view>(); this.mposition = position; this.mconvertview = layoutinflater.from(context).inflate(layoutid, parent, false); this.mconvertview.settag(this); } /** * 拿到一个viewholder对象 * @param context * @param convertview * @param parent * @param layoutid * @param position * @return */ public static viewholder get(context context, view convertview, viewgroup parent, int layoutid, int position) { if (null == convertview) { return new viewholder(context, parent, layoutid, position); } else { viewholder holder = (viewholder) convertview.gettag(); holder.mposition = position; return holder; } } /** * 通过控件的id获取对应的控件,如果没有则加入views * @param viewid * @return */ public <t extends view> t getview(int viewid) { view view = mviews.get(viewid); if (null == view) { view = mconvertview.findviewbyid(viewid); mviews.put(viewid, view); } return (t) view; } /** * 为textview设置字符串 * @param viewid * @param text * @return */ public viewholder settext(int viewid, string text) { textview tv = getview(viewid); tv.settext(text); return this; } /** * 为imageview设置图片 * * @param viewid * @param drawableid * @return */ public viewholder setimageresource(int viewid, int drawableid) { imageview view = getview(viewid); view.setimageresource(drawableid); return this; } public int getposition() { return mposition; } }
再写一个adapter继承于万能适配器commonadapter,还是要写一个自己的adapter,因为一个项目可能会有多个listview,但是每个的item元素,布局都会有所不同的,这个泪用来区分不同的listview与自己所对应的item.这个代码量较少完全可以写成内部类在activity.java中.
package com.example.mylistview.adapter; import java.util.list; import android.content.context; import android.view.view; import android.view.view.onclicklistener; import android.widget.checkbox; import com.example.mylistview.r; import com.example.mylistview.domain.bean; import com.example.mylistview.util.commonadapter; import com.example.mylistview.util.viewholder; public class myadapter extends commonadapter<bean> { public myadapter(context context, list<bean> datas, int layoutid) { super(context, datas, layoutid); // todo auto-generated constructor stub } @override public void convert(viewholder holder, final bean bean) { // todo auto-generated method stub holder.settext(r.id.tv_title, bean.gettitle()) .settext(r.id.tv_desc, bean.getdesc()) .settext(r.id.tv_time, bean.gettime()) .settext(r.id.tv_phone, bean.getphone()); /** * 防止checkbox混乱 */ final checkbox cbox = (checkbox)(holder.getview(r.id.cb)); if (cbox != null) { cbox.setchecked(bean.ischecked()); cbox.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { bean.setchecked(cbox.ischecked()); } }); } } }
优化之后用适配器的时候就简单多了下面是实体类,item和mainactivity.java中的代码:
实体类:
package com.example.mylistview.domain; public class bean { private string title; private string desc; private string time; private string phone; private boolean ischecked; public boolean ischecked() { return ischecked; } public void setchecked(boolean ischecked) { this.ischecked = ischecked; } /** * @param title * @param desc * @param time * @param phone */ public bean(string title, string desc, string time, string phone) { this.title = title; this.desc = desc; this.time = time; this.phone = phone; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getdesc() { return desc; } public void setdesc(string desc) { this.desc = desc; } public string gettime() { return time; } public void settime(string time) { this.time = time; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } }
mainactivity.java:
重点代码:
参数有上下文,集合,和自己对应的item就可以了
adapter = new myadapter(this, mdatas, r.layout.item);
package com.example.mylistview.ui; import java.util.arraylist; import java.util.list; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import com.example.mylistview.r; import com.example.mylistview.adapter.myadapter; import com.example.mylistview.domain.bean; public class mainactivity extends activity { private listview listview; private list<bean> mdatas; /** * 适配器 */ private myadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); initdata(); listener(); } private void listener() { // todo auto-generated method stub listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub startactivity(new intent(mainactivity.this, secondactivity.class)); } }); } private void initdata() { // todo auto-generated method stub mdatas = new arraylist<bean>(); bean bean = new bean("android新技能 get", "android-打造万能的listview和gridview适配器", "2015-08-05", "10086"); mdatas.add(bean); bean = new bean("捡到权志龙一个", "在星巴克捡到权志龙一个", "2015-08-06", "10086"); mdatas.add(bean); bean = new bean("gettop一个", "在韩国首尔捡到top一个", "2015-08-07", "10086"); mdatas.add(bean); adapter = new myadapter(this, mdatas, r.layout.item); listview.setadapter(adapter); } private void initview() { // todo auto-generated method stub listview = (listview) findviewbyid(r.id.listview); } }
item布局:
<?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="wrap_content" android:padding="10dp" > <checkbox android:focusable="false" android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centerinparent="true" /> <textview android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" android:singleline="true" android:text="android新技能 get" /> <textview android:id="@+id/tv_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_margintop="10dp" android:layout_toleftof="@id/cb" android:maxlines="2" android:text="android-打造万能的listview和gridview适配器" android:textcolor="#898989" /> <textview android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_desc" android:layout_margintop="10dp" android:text="2015-08-05" android:textcolor="#898989" android:textsize="12sp" /> <textview android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@id/tv_desc" android:layout_margintop="10dp" android:background="#20793d" android:maxlines="1" android:text="10086" android:padding="4dp" android:textcolor="#fff" /> </relativelayout>
效果图:
用这个去适配另一个不同的布局:
新布局的adapter一样继承自己的万能adapter:commonadapter
package com.example.mylistview.adapter; import java.util.list; import java.util.map; import android.content.context; import com.example.mylistview.r; import com.example.mylistview.util.commonadapter; import com.example.mylistview.util.viewholder; public class secondadapter extends commonadapter<map<string, string>> { public secondadapter(context context, list<map<string, string>> datas, int layoutid) { super(context, datas, layoutid); // todo auto-generated constructor stub } @override public void convert(viewholder holder, map<string, string> t) { // todo auto-generated method stub holder.settext(r.id.tv_item2values, t.get("values")); } }
activity.java代码:
package com.example.mylistview.ui; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import android.app.activity; import android.os.bundle; import android.widget.listview; import com.example.mylistview.r; import com.example.mylistview.adapter.myadapter; import com.example.mylistview.adapter.secondadapter; public class secondactivity extends activity { private listview listview_second; private secondadapter secondadapter; private list<map<string, string>> lists = new arraylist<map<string, string>>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); initview(); initdata(); } private void initdata() { // todo auto-generated method stub for (int i = 0; i < 4; i++) { map map = new hashmap<string, string>(); map.put("values", "条目" + i); lists.add(map); } secondadapter = new secondadapter(this, lists, r.layout.item2); listview_second.setadapter(secondadapter); } private void initview() { // todo auto-generated method stub listview_second = (listview) findviewbyid(r.id.listview_second); } }
item:
<?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="horizontal" > <textview android:id="@+id/tv_item2values" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="ffffff" android:layout_margin="16dp" android:gravity="center" /> </linearlayout>
效果图:
以上就是对android listview 的资料整理,希望能给大家带来帮助!