版权声明:本文为 zhangxiao原创文章,可以随意转载,但必须在明确位置注明出处!!!
转载请标明出处:http://www.jianshu.com/p/2bc7edde983f
1.概述
Adapter写多了就想"偷懒"于是封装了一个通用的Adapter,话不多说上Demo先:https://github.com/zh-xiao/CommonListView.git
2.举个栗子
传统的listview写法:
layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xiao.commonlistview.MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
item_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/head_img"
android:layout_width="50dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
bean:
public class ContactsBean {
private String name;
private String phoneNumber;
private String head_img;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getHead_img() {
return head_img;
}
public void setHead_img(String head_img) {
this.head_img = head_img;
}
}
adapter:
public class ContactsAdapter extends BaseAdapter{
private Context mContext;
private List<ContactsBean> mList;
public ContactsAdapter(Context context, List<ContactsBean> list) {
mContext = context;
mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null==convertView){
convertView= LayoutInflater.from(mContext).inflate(R.layout.item_layout,parent,false);
holder=new ViewHolder();
holder.name= (TextView) convertView.findViewById(R.id.name);
holder.phoneNumber= (TextView) convertView.findViewById(R.id.phone_number);
holder.headImg= (ImageView) convertView.findViewById(R.id.head_img);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
ContactsBean contactsBean = mList.get(position);
holder.name.setText(contactsBean.getName());
holder.phoneNumber.setText(contactsBean.getPhoneNumber());
Picasso.with(mContext).load(contactsBean.getHead_img()).into(holder.headImg);
return convertView;
}
class ViewHolder{
TextView name;
TextView phoneNumber;
ImageView headImg;
}
}
activity:
public class MainActivity extends AppCompatActivity {
private List<ContactsBean> mList=new ArrayList<>();
private ContactsAdapter mAdapter;
private android.widget.ListView lv;
private android.widget.RelativeLayout activitymain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activitymain = (RelativeLayout) findViewById(R.id.activity_main);
this.lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(mAdapter=new ContactsAdapter(this,mList));
initData();
}
public void initData(){
for (int i=0;i<20;i++){
ContactsBean bean=new ContactsBean();
bean.setName("小明");
bean.setPhoneNumber("110");
bean.setHead_img("http://img.qqai.net/uploads/i_2_1826721258x2403292640_21.jpg");
mList.add(bean);
}
mAdapter.notifyDataSetChanged();
}
}
现在我们分析一下传统的写法
既然我们要造一个通用的adapter,那分析一下adapter先:
1.从构造方法到getItemId方法都是一样的写法,也就bean不一样(这部分用一个泛型就可以封装起来).
2.再看getView方法,convertView通过setTag绑定了ViewHolder,而Viewholder又通过findviewById保存了convertView里所有的控件,那我们就可以写一个通用的ViewHolder类,包含两个成员变量mConvertView和mViewMap.mConvertView通过setTag绑定ViewHolder,mViewMap通过<Id,View>键值对保存mConvertView里所有的控件.
OK,思路理清了,开始写代码
通用ViewHolder
public class ViewHolder {
private View mConvertView;
private Map<Integer,View> mViewMap;
private Context mContext;
public ViewHolder(Context context,int layoutId,ViewGroup parent){
mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false);
mViewMap=new HashMap<>();
mConvertView.setTag(this);
mContext=context;
}
public synchronized static ViewHolder getHolder(Context context,int layoutId, View convertView, ViewGroup parent){
if (null==convertView){
return new ViewHolder(context,layoutId,parent);
}else{
return (ViewHolder) convertView.getTag();
}
}
public View getConvertView(){
return mConvertView;
}
/**
* 获取控件,mViewMap里有则直接取,没有则通过findViewById拿到并保存到mViewMap
* @param viewId 控件id
* @param <T>
* @return
*/
public <T extends View> T getView(int viewId){
if (mViewMap.containsKey(viewId)){
return (T) mViewMap.get(viewId);
}else{
View view=mConvertView.findViewById(viewId);
mViewMap.put(viewId,view);
return (T) view;
}
}
/**
* 内容适配,一般就用到ImageView和TextView,而且ImageView通常是url,所以就用反射封装一下,返回自身实现链式调用
* @param viewId 控件id
* @param content 要适配的内容(文字字符串/图片url)
* @return
*/
public ViewHolder set(int viewId,String content){
View view=getView(viewId);
if (view instanceof TextView){
((TextView) view).setText(content);
}else if (view instanceof ImageView){
Picasso.with(mContext).load(content).into((ImageView) view);
}
return this;
}
}
通用adapter
public abstract class CommonAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mList;
protected int mLayoutId;
public CommonAdapter(Context context, List<T> list, int layoutId) {
mContext=context;
mList=list;
mLayoutId=layoutId;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public T getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 根据OO思想,第一行和最后一行不存在变化,所以封装起来,中间适配内容的部分通过convertView抽象方法交给调用者去实现
* @param position
* @param convertView
* @param parent
* @return
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.getHolder(mContext, mLayoutId, convertView, parent);
convertView(holder,mList.get(position));
return holder.getConvertView();
}
/**
* 真正内容适配的方法
* @param holder
* @param t
*/
public abstract void convertView(ViewHolder holder,T t);
}
造轮子的工作就over了,看下现在的listView写法
layout,item_layout,bean不变
adapter由commonAdapter替代
直接看adcitivity:
public class MainActivity extends AppCompatActivity {
private List<ContactsBean> mList = new ArrayList<>();
// private ContactsAdapter mAdapter;
private CommonAdapter mAdapter;
private android.widget.ListView lv;
private android.widget.RelativeLayout activitymain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activitymain = (RelativeLayout) findViewById(R.id.activity_main);
this.lv = (ListView) findViewById(R.id.lv);
// lv.setAdapter(mAdapter=new ContactsAdapter(this,mList));
lv.setAdapter(mAdapter = new CommonAdapter<ContactsBean>(this, mList, R.layout.item_layout) {
@Override
public void convertView(ViewHolder holder, ContactsBean contactsBean) {
holder.set(R.id.name, contactsBean.getName())
.set(R.id.phone_number, contactsBean.getPhoneNumber())
.set(R.id.head_img, contactsBean.getHead_img());
}
});
initData();
}
public void initData() {
for (int i = 0; i < 20; i++) {
ContactsBean bean = new ContactsBean();
bean.setName("小明");
bean.setPhoneNumber("110");
bean.setHead_img("http://img.qqai.net/uploads/i_2_1826721258x2403292640_21.jpg");
mList.add(bean);
}
mAdapter.notifyDataSetChanged();
}
}
是不是超简单,以后只需要一个匿名内部类就OK了.O(∩_∩)O.
参考:http://blog.csdn.net/lmj623565791/article/details/38902805
网友评论