Android Adapter优化之终极版

移动开发中个人认为最重要的就是应用的用户体验度,其中最重要的就是流畅度,而Android的Adapter优化就是很重要的一个方面,下面的代码就是一个Adapter的优化,当然从网上学习了很多大神写的代码,贴出来分享,大家共同进步,有什么好的建议可以留言评论

import java.util.ArrayList;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class BaseArrayListAdapter<T> extends BaseAdapter {

protected ArrayList<T> data;

@Override
public int getCount() {
// TODO Auto-generated method stub
return data == null ? 0 : data.size(); 
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getViewHolder(convertView, parent, position).getConvertView();
}
public abstract ViewHolder getViewHolder(View convertView, ViewGroup parent, int position);

static class ViewHolder {
private final SparseArray<View> views;
private View convertView;

private ViewHolder(Context mContext, View convertView) {
this.views = new SparseArray<View>();
this.convertView = convertView;
convertView.setTag(this);
}

public static ViewHolder get(Context mContext, View convertView, ViewGroup parent, int resId) {
if(convertView == null) {
LayoutInflater factory = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = factory.inflate(resId, parent, false);//
return new ViewHolder(mContext, convertView);
} 
return (ViewHolder) convertView.getTag();
}

@SuppressWarnings("unchecked")
public <T extends View> T findViewById(int resourceId) {
View view = views.get(resourceId);
if (view == null) {
view = convertView.findViewById(resourceId);
views.put(resourceId, view);
}
return (T) view;
}

public View getConvertView() {
return convertView;
}
public void setConvertView(View convertView) {
this.convertView = convertView;
}
}
}
阅读更多
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Android Adapter优化之终极版

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭