首页 > 代码库 > Android ListView优化 如何省略ViewHolder方法

Android ListView优化 如何省略ViewHolder方法

代码:
public class ViewHolder {
    /**
     * @param view converView
     * @param id 控件的id
     * @return 返回<T extends View>
     */
    public static <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        if (null == viewHolder) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (null == childView) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);

        }
        return (T) childView;
    }

}

在Adapter使用:

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String keyName = getItem(position);
        if (null == convertView) {
            convertView = inflate(R.layout.layout_comm_custom_search_item, null);
        }
        TextView searchKeyName = (TextView)ViewHolder.get(convertView, R.id.search_key_name);
        searchKeyName.setText(keyName);
        return convertView;
    }


Android ListView优化 如何省略ViewHolder方法