超级adapter大大减少adapter代码-创新互联
package com.clsys.adapter; import java.util.ArrayList; import com.clsys.activity.R; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class SuperLVAdapterextends BaseAdapter { private final int mIdResource; private Context mContext; private ArrayList mArrayList; public ViewHolderBase holder = new ViewHolderBase(); public SuperLVAdapter(Context mContext, int mIdResource, ArrayList mArrayList) { this.mContext = mContext; this.mIdResource = mIdResource; this.mArrayList = mArrayList; } @Override public int getCount() { // TODO Auto-generated method stub return mArrayList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mArrayList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(mIdResource, null); } initViewContent(convertView); return convertView; } public void initViewContent(View convertView) { } class ViewHolderBase { // I added a generic return type to reduce the casting noise in client // code @SuppressWarnings("unchecked") public T get(View view, int id) { SparseArray viewHolder = (SparseArray ) view.getTag(); if (viewHolder == null) { viewHolder = new SparseArray (); view.setTag(viewHolder); } View childView = viewHolder.get(id); if (childView == null) { childView = view.findViewById(id); viewHolder.put(id, childView); } return (T) childView; } } }
具体用法:
创新互联主要从事成都网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务铅山,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792继承此类后 重写下面这个方法就好了 赶紧试试吧
public void initViewContent(View convertView) { TextView tvTextView = holder.get(convertView, R.id.tv);tv.settext("测试");}
欢迎关注公众账号 jszj2014215 分享android资讯和技术相关问题
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页名称:超级adapter大大减少adapter代码-创新互联
网页地址:http://scgulin.cn/article/pojoh.html