首页 > 代码库 > 超级adapter 大大减少adapter代码
超级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 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资讯和技术相关问题
超级adapter 大大减少adapter代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。