Android 使得ListView滑動順暢

2018-08-02 18:26 更新

編寫:allenlsy - 原文:http://developer.android.com/training/improving-layouts/smooth-scrolling.html

保持程序流暢的關(guān)鍵,是讓主線程(UI 線程)不要進行大量運算。你要確保在其他線程執(zhí)行磁盤讀寫、網(wǎng)絡(luò)讀寫或是 SQL 操作等。為了測試你的應(yīng)用的狀態(tài),你可以啟用 StrictMode。

使用后臺線程

你應(yīng)該把主線程中的耗時間的操作,提取到一個后臺線程(也叫做“worker thread工作線程”)中,使得主線程只關(guān)注 UI 繪畫。很多時候,使用 AsyncTask 是一個簡單的在主線程以外進行操作的方法。系統(tǒng)會自動把execute()的請求放入隊列中并線性調(diào)用執(zhí)行。這個行為是全局的,這意味著你不需要考慮自己定義線程池的事情。

在下面的例子中,一個 AsyncTask 被用于在后臺線程載入圖片,并在載入完成后把圖片顯示到 UI 上。當圖片正在載入時,它還會顯示一個進度提示。

// Using an AsyncTask to load the slow images in a background thread
new AsyncTask<ViewHolder, Void, Bitmap>() {
    private ViewHolder v;

    @Override
    protected Bitmap doInBackground(ViewHolder... params) {
        v = params[0];
        return mFakeImageLoader.getImage();
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        if (v.position == position) {
            // If this item hasn't been recycled already, hide the
            // progress and set and show the image
            v.progress.setVisibility(View.GONE);
            v.icon.setVisibility(View.VISIBLE);
            v.icon.setImageBitmap(result);
        }
    }
}.execute(holder);

從 Android 3.0 (API level 11) 開始, AsyncTask 有個新特性,那就是它可以在多個 CPU 核上運行。你可以調(diào)用 executeOnExecutor()而不是execute(),前者可以根據(jù)CPU的核心數(shù)來觸發(fā)多個任務(wù)同時進行。

在 ViewHolder 中填入視圖對象

你的代碼可能在 ListView 滑動時經(jīng)常使用 findViewById(),這樣會降低性能。即使是 Adapter 返回一個用于回收的 inflate 后的視圖,你仍然需要查看這個元素并更新它。避免頻繁調(diào)用 findViewById() 的方法之一,就是使用 ViewHolder(視圖占位符)的設(shè)計模式。

一個 ViewHolder 對象存儲了他的標簽下的每個視圖。這樣你不用頻繁查找這個元素。第一,你需要創(chuàng)建一個類來存儲你會用到的視圖。比如:

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;
}

然后,在 Layout 的類中生成一個 ViewHolder 對象:

ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);

這樣你就可以輕松獲取每個視圖,而不是使用 findViewById() 來不斷查找子視圖,節(jié)省了寶貴的運算時間。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號