Android ApiDemos樣本解析(180):Views->Lists->13. Sl

來源:互聯網
上載者:User

對於一個長列表,如果每個清單項目載入比較費時( 比如從網路下載映像),如果此時需要快速滾動列表,可以先給清單項目使用一個替代名稱或是其它可以快速載入的項,當列錶停止滾動時再載入實際的清單項目,這樣可以大大提高列表的響應效能。

本例在ListView中顯示一個字串數組來類比一個費時的清單項目載入(儘管顯示文字清單項目實際上很快),當列表開始滾動或是快速飛行時,使用一個臨時資料來繫結資料行表項(如Loading …),而在列錶停止滾動時,使用實際需要顯示的文字替換之前顯示的臨時資料。

看看自訂SlowAdapter的getView 的實現:


[java] 
public View getView(int position, 
 View convertView, ViewGroup parent) { 
 TextView text; 
 
 if (convertView == null) { 
 text = (TextView)mInflater.inflate 
 (android.R.layout.simple_list_item_1, 
 parent, false); 
 } else { 
 text = (TextView)convertView; 
 } 
 
 if (!mBusy) { 
 text.setText(mStrings[position]); 
 // Null tag means the view has the c  
 //orrect data  
 text.setTag(null); 
 } else { 
 text.setText("Loading..."); 
 // Non-null tag means the view still  
 //needs to load it's data  
 text.setTag(this); 
 } 
 
 return text; 
} 

public View getView(int position,
 View convertView, ViewGroup parent) {
 TextView text;

 if (convertView == null) {
 text = (TextView)mInflater.inflate
 (android.R.layout.simple_list_item_1,
 parent, false);
 } else {
 text = (TextView)convertView;
 }

 if (!mBusy) {
 text.setText(mStrings[position]);
 // Null tag means the view has the c
 //orrect data
 text.setTag(null);
 } else {
 text.setText("Loading...");
 // Non-null tag means the view still
 //needs to load it's data
 text.setTag(this);
 }

 return text;
}

可以看到在ListView busy(滾動時) ,將textview 的文字設定成臨時資料(loading …”) ,並把textView 的tag 設為非空, TextView 的Tag 可以使用任何對象,本例用它來做個標記,非空時表示當前TextView使用的是臨時資料。 而的ListView不忙時(停止滾動),將TextView設定成實際需要顯示的文字(或是其它費時的操作),並把它的Tag設為空白,表示TextView顯示的是真實資料。

為ListView 添加滾動事件處理


[java] 
getListView().setOnScrollListener(this);} 
... 
 
public void onScrollStateChanged(AbsListView view, 
 int scrollState) { 
 switch (scrollState) { 
 case OnScrollListener.SCROLL_STATE_IDLE: 
 mBusy = false; 
 
 int first = view.getFirstVisiblePosition(); 
 int count = view.getChildCount(); 
 for (int i=0; i<count; i++) { 
 TextView t = (TextView)view.getChildAt(i); 
 if (t.getTag() != null) { 
 t.setText(mStrings[first + i]); 
 t.setTag(null); 
 } 
 } 
 
 mStatus.setText("Idle"); 
 break; 
 case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: 
 mBusy = true; 
 mStatus.setText("Touch scroll"); 
 break; 
 case OnScrollListener.SCROLL_STATE_FLING: 
 mBusy = true; 
 mStatus.setText("Fling"); 
 break; 
 } 
} 

getListView().setOnScrollListener(this);}
...

public void onScrollStateChanged(AbsListView view,
 int scrollState) {
 switch (scrollState) {
 case OnScrollListener.SCROLL_STATE_IDLE:
 mBusy = false;

 int first = view.getFirstVisiblePosition();
 int count = view.getChildCount();
 for (int i=0; i<count; i++) {
 TextView t = (TextView)view.getChildAt(i);
 if (t.getTag() != null) {
 t.setText(mStrings[first + i]);
 t.setTag(null);
 }
 }

 mStatus.setText("Idle");
 break;
 case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
 mBusy = true;
 mStatus.setText("Touch scroll");
 break;
 case OnScrollListener.SCROLL_STATE_FLING:
 mBusy = true;
 mStatus.setText("Fling");
 break;
 }
}

在ListView 滾動或是飛速滑動時,將busy設為true, 當滾動停止時SCROLL_STATE_IDLE ,busy為false, 並根據TextView的Tag值判斷是否需要顯示實際的文字串。

本例最下面並使用了一個文字框顯示了當前ListView的滾動狀態。

 

 

 


 

 

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.