学无止境,感谢朋友的分享,让我的逼格又提升了一点点。
你的适配器代码是这样的吗?
package com.customeradapter.adapter; import java.util.List; import android.content.Context; import android.text.Html; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; import com.customeradapter.R; import com.customeradapter.bean.Person; public class DataAdapter extends BaseAdapter { private Context context; private List<Person> lstPerson; public DataAdapter(Context context, List<Person> lstPerson) { this.context = context; this.lstPerson = lstPerson; } @Override public int getCount() { return lstPerson == null ? 0 : lstPerson.size(); } @Override public Object getItem(int position) { return lstPerson == null ? 0 : lstPerson.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LinearLayout.inflate(context, R.layout.item_data, parent); viewHolder.tvId = (TextView) convertView.findViewById(R.id.tv_id); viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name); viewHolder.tvAge = (TextView) convertView.findViewById(R.id.tv_age); viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Person person = lstPerson.get(position); viewHolder.tvId.setText(String.valueOf(person.getId())); viewHolder.tvName.setText(person.getName()); viewHolder.tvAge.setText(String.valueOf(person.getAge())); viewHolder.tvContent.setText(Html.fromHtml(person.getContent())); return null; } class ViewHolder { private TextView tvId; private TextView tvName; private TextView tvAge; private TextView tvContent; } }
public class DataAdapter extends CommonAdapter<Person> { public DataAdapter(Context context, List<Person> datas, int layoutId) { super(context, datas, R.layout.item_data); } @Override public void convert(CommonViewHolder holder, Person t, List<Person> datas) { TextView tvId = holder.getView(R.id.tv_id); TextView tvName = holder.getView(R.id.tv_name); TextView tvAge = holder.getView(R.id.tv_age); TextView tvContent = holder.getView(R.id.tv_content); tvId.setText(String.valueOf(t.getId())); tvName.setText(t.getName()); tvAge.setText(String.valueOf(t.getAge())); tvContent.setText(Html.fromHtml(t.getContent())); } }
“减少重复代码块。”
有人会说了,这是适配器吗?跟我平常的不一样啊。
常用的getCount()getItem()getView()哪去了?
大家不用担心,和你们平常写的没区别,代码差不多,只不过是把这几个重复代码在
CommonAdapter<Person> 里面做了一层封装,
其实万能适配器的原型是这样的CommonAdapter<T>,让我们看看它的真实面目:
package com.customeradapter.adapter; 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 { protected Context context; protected LayoutInflater inflater; protected List<T> datas; protected int layoutId; public CommonAdapter(Context context, List<T> datas, int layoutId) { inflater = LayoutInflater.from(context); this.context = context; this.datas = datas; this.layoutId = layoutId; } @Override public int getCount() { return datas.size(); } @Override public T getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { CommonViewHolder holder = CommonViewHolder.get(context, convertView, parent, layoutId, position); convert(holder, getItem(position), datas); return holder.getmConvertView(); } public abstract void convert(CommonViewHolder holder, T t, List<T> datas); }
的,所以做一层封装对于大多数的listview都能够满足,这里的改造还有很多方法,能不能适配各种奇葩的
listview就要靠大家动脑了,这里就做一个抛砖引玉的效果了。然后将你要写的adapter继承与这个抽象的
CommonAdapter<T>,T是需要传入Adapter的集合里面的数据类型,一般都是自定义的类型,我这里用到的
是Person。最后需要重写的方法,会自动生成的。
下面给出免费的Demo地址:
http://download.csdn.net/detail/zhouhui520w/9124823