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,int position) { if (null == convertVIEw) { return new VIEwHolder(context,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,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,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,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 的资料整理,希望能给大家带来帮助!
总结以上是内存溢出为你收集整理的Android ListView万能适配器实例代码全部内容,希望文章能够帮你解决Android ListView万能适配器实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)