关于在定制阵列适配器的getView方法中回收视图,我有一个不清楚的问题.
我明白元素被重用,但是如何在if语句的第一部分中确切地知道要实现的内容,第二个是什么呢?
现在我正在使用以下代码.我来到这个问题,因为在语句的第二部分中删除了代码,导致了前9个元素的列表,这些元素是重复次数而不是所有元素.我真的不知道究竟是什么导致的…
@Override public View getView(int position,View convertView,ViewGroup parent) { View row = convertView; if (row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId,parent,false); title = getItem(position).getTitle(); size = calculateFileSize(position); txtTitle = (TextView) row.findViewById(R.id.txtTitle); tvFileSize = (TextView) row.findViewById(R.id.tvFileSize); txtTitle.setText(title); tvFileSize.setText(size); } else { title = getItem(position).getTitle(); size = calculateFileSize(position); txtTitle = (TextView) row.findViewById(R.id.txtTitle); tvFileSize = (TextView) row.findViewById(R.id.tvFileSize); txtTitle.setText(title); tvFileSize.setText(size); } return row; }
解决方法
I understand that elements are reused,but how do I know exact what to implement in the first part of the if statement,and what in the second?
一旦你得到它的组织是非常简单的:
public View getView(int position,ViewGroup parent) { if (convertView == null) { /* This is where you initialize new rows,by: * - Inflating the layout,* - Instantiating the ViewHolder,* - And defining any characteristics that are consistent for every row */ } else { /* Fetch data already in the row layout,* primarily you only use this to get a copy of the ViewHolder */ } /* Set the data that changes in each row,like `title` and `size` * This is where you give rows there unique values. */ return convertView; }
有关ListView的RecycleBin的工作原理以及ViewHolders为什么重要的Turbo Charge your UI的详细说明,Android的领先的ListView程序员的Google I / O演示.