ListView的複用和快取機制
複用分析
**1. 在AbListView中 **
/**
* The data set used to store unused views that should be reused during the next layout
* to avoid creating new ones
*/
final RecycleBin mRecycler = new RecycleBin();
RecycleBin包含了兩個級別的儲存,ActiveViews和ScrapViews,ActiveViews儲存的是第一次顯示在螢幕上的View;所有的ActiveViews最終都會被移到ScrapViews,ScrapViews儲存的是有可能被adapter複用的View。
**2. 在ListView的LayoutChildren 中 **
@Override
protected void layoutChildren()
{
if (dataChanged)
{
for (int i = 0; i < childCount; i++)
{
recycleBin.addScrapView(getChildAt(i));
}
} else
{
recycleBin.fillActiveViews(childCount, firstPosition);
}
....
}
可以看出,如果資料發生變化則把當前的ItemView放入ScrapViews中,否則把當前顯示的ItemView放入ActiveViews中。
3. 那麼我們們關鍵的getView方法到底是在哪呼叫呢
在AbsListView中我們看obtainView中的方法:
/**
* Get a view and have it show the data associated with the specified
* position. This is called when we have already discovered that the view is
* not available for reuse in the recycle bin. The only choices left are
* converting an old view or making a new one.
*
* @param position The position to display
* @param isScrap Array of at least 1 boolean, the first entry will become true if
* the returned view was taken from the scrap heap, false if otherwise.
*
* @return A view displaying the data associated with the specified position
*/
View obtainView(int position, boolean[] isScrap)
{
final View scrapView = mRecycler.getScrapView(position);
final View child = mAdapter.getView(position, scrapView, this);
if (scrapView != null) {
if (child != scrapView) {
// Failed to re-bind the data, return scrap to the heap.
mRecycler.addScrapView(scrapView, position);
} else {
isScrap[0] = true;
child.dispatchFinishTemporaryDetach();
}
}
return child;
}
可以看到,這個方法就是返回當前一個佈局使用者當前Item的顯示,首先根據position去ScrapView中找,找到後呼叫我們的getView,此時getView裡面的convertView!=null了,然後getView如果返回的View發生變化,快取下來,否則convertView==null了。
參考資料
相關文章
- RecyclerView快取機制(咋複用?)View快取
- 再次探究Android ListView快取機制AndroidView快取
- 深入理解Android中的快取機制(二)RecyclerView跟ListView快取機制對比Android快取View
- getView()不復用convertView,ListView即毫無複用!(ListView回收機制)View
- 對於前端快取的理解(快取機制和快取型別)前端快取型別
- 動態 SQL 和快取機制SQL快取
- mybatis的快取機制MyBatis快取
- mysql的快取機制MySql快取
- MyBatis 的快取機制MyBatis快取
- (四)Redis 快取應用、淘汰機制Redis快取
- 從WebView快取聊到Http 的快取機制WebView快取HTTP
- Web 快取機制 與 快取策略Web快取
- HTTP快取機制HTTP快取
- Mybatis快取機制MyBatis快取
- LRU快取機制快取
- 前端快取機制前端快取
- web快取機制Web快取
- client快取機制client快取
- Web快取知多少(快取機制和資料儲存)Web快取
- 圖解 HTTP 的快取機制 | 實用 HTTP圖解HTTP快取
- node中的快取機制快取
- MyBatis快取機制(一級快取,二級快取)MyBatis快取
- 可複用的快取元件快取元件
- http系列--徹底理解瀏覽器的快取機制(http快取機制)HTTP瀏覽器快取
- 瀏覽器的快取機制—強快取與協商快取瀏覽器快取
- 視覺化 ListView 快取機制,手摸手帶你打通任督二脈視覺化View快取
- 聊聊資料庫和快取同步機制資料庫快取
- Redis 快取失效機制Redis快取
- 瀏覽器的快取機制瀏覽器快取
- Redis 的快取淘汰機制(Eviction)Redis快取
- Android webview的快取機制AndroidWebView快取
- RecyclerView的複用機制View
- ListView 中的 RecycleBin 機制View
- Nginx快取原理及機制Nginx快取
- RecyclerView快取機制(scrap view)View快取
- HTTP快取機制及原理HTTP快取
- 瀏覽器快取機制瀏覽器快取
- yii2 快取機制快取