一、adapter中getview得简化写法(参考农民伯伯的博客):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_feed_item, parent, false);
}
ImageView thumnailView = getAdapterView(convertView, R.id.video_thumbnail);
ImageView avatarView = getAdapterView(convertView, R.id.user_avatar);
ImageView appIconView = getAdapterView(convertView, R.id.app_icon);
}
public static <T extends View> T getAdapterView(View convertView, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
convertView.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = convertView.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
Adapter中getView简化
本文介绍了一种在Android开发中Adapter的getView方法的简化写法,通过缓存视图并复用,提高ListView或RecyclerView的加载效率。具体实现包括使用ViewHolder模式,减少findViewById的调用。

被折叠的 条评论
为什么被折叠?



