簡體   English   中英

兩種技術在Android中回收列表行視圖之間的區別

[英]Difference between two techniques recycling list row views in Android

我知道關於回收性能列表中的行的方法。 我通常看到的是使用static類和標簽的一種技術( viewHolder

例如:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;

    if ( convertView == null )
    {
        convertView = mInflater.inflate(R.id.row, null);  
        holder = new ViewHolder();
        holder.txt1 = (TextView) convertView.findViewById( R.id.txt1 );
        holder.txt2 = (TextView) convertView.findViewById( R.id.txt2 );
        holder.txt3 = (TextView) convertView.findViewById( R.id.txt3 );
// setting more images,images
        convertView.setTag (holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag ();
    }

    holder.txt1.setText( data.get( position ).txt1 );
    holder.txt2.setText( data.get( position ).txt2 );
    holder.txt3.setText( data.get( position ).txt3 );

    return convertView;
}

static class ViewHolder{
    TextView txt1;
    TextView txt2;
    TextView txt3;
}

但是在某些代碼中,我看到了一種更簡單的方法,該方法不使用static類和標記,它只是檢查視圖是否被回收(如果是),如果不創建則使用它。

 public View getView(int position, View convertView, ViewGroup parent) {
                if(convertView == null){
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.history_row, null);
                }
        LinearLayout row = (LinearLayout) convertView.findViewById(R.id.row);

      TextView txt1 = (TextView) convertView.findViewById(R.id.txt1);
                    txt1.setText(data.getTxt1());
     TextView txt2 = (TextView) convertView.findViewById(R.id.txt1);
                    txt2.setText(data.getTxt2());
     TextView txt3 = (TextView) convertView.findViewById(R.id.txt2);
                    txt3.setText(data.getTxt3());

    }

它們之間有什么區別?使用哪種更好?

findViewById()是一個昂貴的調用。 一個人應該避免使用它。 在第一種方法中,將為所有新創建的視圖而不是任何convertView調用findViewById() 在第二種方法中,為所有視圖調用findViewById() 無疑,第一種方法要好用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM