ListView深入理解:效能最佳化1,listview深入理解

來源:互聯網
上載者:User

ListView深入理解:效能最佳化1,listview深入理解
一列表顯示三要素:

1.ListView:用於顯示列表的view;

2.Adapter:  將資料對應到ListView;

3.Data:     具體的將要被映射的字串、圖片或其他基本組件;


二最佳化原理:1.只建立必要的view對象;

  ListView載入資料的方式:

  首先調用getCount()函數,根據返回值得到listView的長度;

  然後根據這個長度,調用getView()擷取listView的每一項item所對應的view。

  如果每一個item都去建立新的對象,必然會佔據過多的記憶體。

2.儘可能減少去執行Layout的inflate操作;

Infalte操作會利用xml檔案執行個體化相應的View,屬於IO操作,是耗時操作。

3.減少findViewById()操作;通過findViewById()方法可以在一個xml檔案中尋找view。這個方法會遞迴整個view樹,以尋找與ID相匹配的view。

當item中含有較複雜的布局時,頻繁的回調findViewById()方法無疑會影響滑動的效能。


三最佳化機制:1.Recycler(實現原理1和原理2)

  當listView的item離開當前的視圖時,對應的view會被緩衝在Recycler中。

這時會相應的從下方產生一個item,而此時調用的getView()函數中的convertView參數就是緩衝的view。

所以listView效能最佳化的關鍵在於重用convertView。




2.viewHolder靜態類(實現原理3)  將view的xml中的元素封裝成ViewHolder靜態類,通過convertView的setTag將view與相應的holder對象綁定在一起。避免大量的findViewById()操作。

TagsUnlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view.                   They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

(利用tag關聯view的額外資訊)


四最佳化過程:1.最初寫法
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);ImageView imageView1 = (ImageView)view.findViewById(R.id.imageView1);TextView textView2 = (TextView)view.findViewById(R.id.text2);imageView1.setImageResource(R.drawable.ic_launcher);textView2.setText(mData.get(position));return view;}
(存在問題:1.建立view過多;2.inflate操作過多;3.findViewById()調用次數過多)


2.重用convertView
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView == null){convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);}ImageView imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);TextView textView2 = (TextView)convertView.findViewById(R.id.text2);imageView1.setImageResource(R.drawable.ic_launcher);textView2.setText(mData.get(position));return convertView;}
利用Recycler機制,成功解決問題1和問題2;


3.利用ViewHolder靜態類
static class ViewHolder{ImageView imageView1;TextView textView1;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView == null){holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);holder.imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);holder.textView1 = (TextView)convertView.findViewById(R.id.text1);convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}holder.imageView1.setImageResource(R.drawable.ic_launcher);holder.textView1.setText(mData.get(position));return convertView;}
利用viewHolder靜態類,成功解決問題3;

(建議將ViewHolder寫為靜態類,因為用static定義的靜態內部類相對獨立,不能訪問所在類的方法和實體,佔用資源更少。如果去掉static,可以彼此互相訪問實體,會浪費一些資源。)


4.利用依賴注入架構精簡代碼:
static class ViewHolder{@InjectView(R.id.imageView1)ImageView imageView1;@InjectView (R.id.text1)TextView textView1;}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView == null){holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}holder.imageView1.setImageResource(R.drawable.ic_launcher);holder.textView1.setText(mData.get(position));return convertView;}
依賴注入架構能幫你精簡應用程式的代碼。代碼越少意味著出現問題或bug的次數也就越少,從而可以把更多的精力花在項目中那些需要編寫或修改的部分,使得閱讀代碼更加容易。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.