您好,登錄后才能下訂單哦!
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; } } }
具體用法:
繼承此類后 重寫下面這個(gè)方法就好了 趕緊試試吧
public void initViewContent(View convertView) { TextView tvTextView = holder.get(convertView, R.id.tv);tv.settext("測試");}
歡迎關(guān)注公眾賬號(hào) jszj2014215 分享android資訊和技術(shù)相關(guān)問題
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。