Skip to content

Instantly share code, notes, and snippets.

@jimbray
Created March 4, 2016 09:12
Show Gist options
  • Save jimbray/219ae562e5885ad0989a to your computer and use it in GitHub Desktop.
Save jimbray/219ae562e5885ad0989a to your computer and use it in GitHub Desktop.
通用的ListView Adapter
package com.zrd.waukeen.common;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CommonViewHolder {
private SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
public CommonViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mConvertView.setTag(this);
}
public static CommonViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if(convertView == null) {
return new CommonViewHolder(context, parent, layoutId, position);
} else {
CommonViewHolder holder = (CommonViewHolder)convertView.getTag();
holder.mPosition = position;
return holder;
}
}
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if(view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T)view;
}
public View getConvertView() {
return mConvertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment