超级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; } } }
具体用法:
创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目成都网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元太白做网站,已为上家服务,为太白各地企业和个人服务,联系电话:13518219792
继承此类后 重写下面这个方法就好了 赶紧试试吧
public void initViewContent(View convertView) { TextView tvTextView = holder.get(convertView, R.id.tv);tv.settext("测试");}
欢迎关注公众账号 jszj2014215 分享android资讯和技术相关问题
本文题目:超级adapter大大减少adapter代码
链接分享:http://ybzwz.com/article/iedesd.html