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 SuperLVAdapter<T> extends BaseAdapter { private final int mIdResource; private Context mContext; private ArrayList<T> mArrayList; public ViewHolderBase holder = new ViewHolderBase(); public SuperLVAdapter(Context mContext, int mIdResource, ArrayList<T> 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 extends View> T get(View view, int id) { SparseArray<View> viewHolder = (SparseArray<View>) view.getTag(); if (viewHolder == null) { viewHolder = new SparseArray<View>(); view.setTag(viewHolder); } View childView = viewHolder.get(id); if (childView == null) { childView = view.findViewById(id); viewHolder.put(id, childView); } return (T) childView; } } }
具体用法:
继承此类后 重写下面这个方法就好了 赶紧试试吧
public void initViewContent(View convertView) { TextView tvTextView = holder.get(convertView, R.id.tv);tv.settext("测试");}
欢迎关注公众账号 jszj2014215 分享android资讯和技术相关问题
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。