2012年10月10日 星期三

[Android] ListView Tips

In order to avoid blending and make the rendering faster while scrolling/flinging, the ListView widget uses a ’scrolling cache’ mechanism.
  • ListView 背景預設為透明色,所以會看到 window 預設的背景色,深灰色 (#FF191919)
  • 為了滑動效能,the Android framework reuses the cache color hint.
  • 若 scrolling cache is true,那麼當 color hint is set,the framework 複製 ListView 中的每個 child 為 bimap 並且設該 hint value.
    • the framework copies each child of the list in a Bitmap filled with the hint value. ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. (?)
    • This mechanism consists on taking an opaque screenshot of all visible child Views (in other words, it draws the content of each itemview in a Bitmap) and directly draws those screenshots when necessary. 
  • 因為預設的 cache color hint is #191919,所以在滑動時會看到 item 的黑色背景。
    • The color used to make those screenshots opaque is the one given by the cache color hint.
    • If your ListView turns opaque (black for instance) when scrolling that because you need to set the cache color hint to the actual color of your ListView background.
若不想看到這黑色(cache color hint)背景(也就是關閉此優化方式),可以在 xml 設置 android:cacheColorHint="#00000000" 即可。


CharArrayBuffer
Cursor#getString() 每次呼叫都會建立 String objects,而且這些 objects 當 user scroll ListView 時就會被回收,所以為避免這樣的建立與回收物件,CharArrayBuffer 儲存 cursor 中的 data,然後直接從 CharArrayBuffer 中存取,reuse。


Using a color as part of your list selector may entirely fill your ListView.
This problem comes from the fact a ColorDrawable does not respect its bounds: it entirely fills the clip rect of the Canvas in which it is drawn into.

Solution
Use a Drawable that respect its bounds such as a GradientDrawable or a NinePatchDrawable.

 
   
* Reference
- AndroidDevBlog » ListView Tips & Tricks #2: Sectioning your ListView
- AndroidDevBlog » ListView Tips & Tricks #3: Create fancy ListViews
- Why is my list black? An Android optimization | Android Developers Blog

沒有留言:

張貼留言