android – 自定义阵列适配器中的回收视图:它是如何处理的?

前端之家收集整理的这篇文章主要介绍了android – 自定义阵列适配器中的回收视图:它是如何处理的?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于在定制阵列适配器的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演示.

原文链接:https://www.f2er.com/android/311498.html

猜你在找的Android相关文章