ViewHolder模式超简洁写法
public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false); TextView name = ViewHolder.get(convertView, R.id.tv_name); name.setText(""); return convertView; } class ViewHolder { // I added a generic return type to reduce the casting noise in client code @SuppressWarnings("unchecked") public static <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; } }
值得注意的是SparseArray这个知识点,优化过的存储integer和object键值对的hashmap
相关推荐
chenjinlong 2020-02-03
yinbaoshiguang 2019-06-27
Palingenesis 2019-06-21
OliverLau 2019-06-20
liuhedev 2018-04-16
85457218 2016-03-06
yffhhffv 2015-10-13
89457902 2015-09-08
84417619 2014-05-09
RungBy 2014-01-02
Leo的memo 2012-12-12
82500190 2012-07-28
89457902 2012-07-17
guizhongyun 2012-04-12
85457218 2011-09-05
wangkuifeng0 2011-04-18
扑克投资家 2018-02-05
明学的白板 2018-01-25
优主张 2018-01-16