Android
パフォーマンスの最適化
サーチ…
前書き
Appsのパフォーマンスは、ユーザーエクスペリエンスの重要な要素です。 UIスレッドで作業するような悪い実行パターンを避け、高速で反応性の高いアプリケーションを作成する方法を学びましょう。
ViewHolderパターンを使用してViewルックアップを保存する
特にListViewでは、スクロール中にfindViewById()呼び出しを多すぎると、パフォーマンス上の問題が発生する可能性があります。 ViewHolderパターンを使用すると、これらの参照を保存してListViewパフォーマンスを向上させることができます。
リスト項目に1つのTextViewが含まれている場合は、インスタンスを格納するViewHolderクラスを作成します。
static class ViewHolder {
TextView myTextView;
}
リスト項目を作成するときに、リスト項目にViewHolderオブジェクトを添付します。
public View getView(int position, View convertView, ViewGroup parent) {
Item i = getItem(position);
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
// Create a new ViewHolder and save the TextView instance
ViewHolder holder = new ViewHolder();
holder.myTextView = (TextView)convertView.findViewById(R.id.my_text_view);
convertView.setTag(holder);
}
// Retrieve the ViewHolder and use the TextView
ViewHolder holder = (ViewHolder)convertView.getTag();
holder.myTextView.setText(i.getText());
return convertView;
}
このパターンを使用すると、 findViewById()は新しいViewが作成されているときにのみ呼び出され、 ListViewはより効率的にListViewをリサイクルできます。
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow