plugin android world of listview android
TRANSCRIPT
![Page 1: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/1.jpg)
Tuesday, June 1, 2010
![Page 2: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/2.jpg)
Tuesday, June 1, 2010
![Page 3: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/3.jpg)
The World of List ViewRomain Guy and Adam PowellMay 19, 2010
Tuesday, June 1, 2010
![Page 4: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/4.jpg)
Tuesday, June 1, 2010
![Page 5: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/5.jpg)
View live notes and ask questions about this session on Wave
http://bit.ly/9zozBR
Tuesday, June 1, 2010
![Page 6: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/6.jpg)
Tuesday, June 1, 2010
![Page 7: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/7.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
5
Tuesday, June 1, 2010
![Page 8: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/8.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
6
Tuesday, June 1, 2010
![Page 9: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/9.jpg)
Google Confidential
Virtualization
• Problem: large data sets– Memory– Performance
• Solution– Populate on demand– Recycle views to reduce object churn
7
Tuesday, June 1, 2010
![Page 10: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/10.jpg)
Google Confidential
Adapters
• Terminology– index: Child views– position: Data in Adapter– id: Unique identifier for data
• Stable IDs– hasStableIds() == true– An ID always refers to the same value– Helps ListView
8
Tuesday, June 1, 2010
![Page 11: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/11.jpg)
Google Confidential
Adapters
• getView(int position, View convertView, ViewGroup parent)– Full data presentation control– Optimization– Shoot yourself in the foot (and the face)
9
Tuesday, June 1, 2010
![Page 12: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/12.jpg)
Google Confidential
Opportunities for optimizationgetView
• ListView is smart• convertView
– Supplied by ListView– Matches item types– Reuse it
10
Tuesday, June 1, 2010
![Page 13: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/13.jpg)
Google Confidential
The Slow WaygetView
11
1 public View getView(int position, View convertView, ViewGroup parent) { 2 View item = mInflater.inflate(R.layout.list_item_icon_text, null); 3 ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); 4 ((ImageView) item.findViewById(R.id.icon)).setImageBitmap( 5 (position & 1) == 1 ? mIcon1 : mIcon2);
6 return item; 7 }
Tuesday, June 1, 2010
![Page 14: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/14.jpg)
Google Confidential
The Right WaygetView
12
1 public View getView(int position, View convertView, ViewGroup parent) { 2 if (convertView == null) { 3 convertView = mInflater.inflate(R.layout.item, parent, false); 4 }
5 ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); 6 ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap( 7 (position & 1) == 1 ? mIcon1 : mIcon2);
8 return convertView; 9 }
Tuesday, June 1, 2010
![Page 15: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/15.jpg)
Google Confidential
The Fast WaygetView
13
static class ViewHolder { TextView text; ImageView icon; }
Tuesday, June 1, 2010
![Page 16: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/16.jpg)
Google Confidential
The Fast WaygetView
14
1 public View getView(int position, View convertView, ViewGroup parent) { 2 ViewHolder holder; 3 4 if (convertView == null) { 5 convertView = mInflater.inflate(R.layout.list_item_icon_text, 6 parent, false); 7 holder = new ViewHolder(); 8 holder.text = (TextView) convertView.findViewById(R.id.text); 9 holder.icon = (ImageView) convertView.findViewById(R.id.icon); 10 11 convertView.setTag(holder); 12 } else { 13 holder = (ViewHolder) convertView.getTag(); 14 } 15 16 holder.text.setText(DATA[position]); 17 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 18 19 return convertView; 20 }
Tuesday, June 1, 2010
![Page 17: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/17.jpg)
Google Confidential
getView
15
0
10
20
30
40
50
60
Dumb Correct Fast
List of 10,000 items, Nexus One device
Tuesday, June 1, 2010
![Page 18: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/18.jpg)
Google Confidential
How to shoot yourself in the footAdapters
• Local view cache• Accessing views from the adapter• Change convertView’s structure• Assumptions about getView calls
16
Tuesday, June 1, 2010
![Page 19: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/19.jpg)
Google Confidential
Handling data changesAdapters
• notifyDataSetChanged() – New or updated data
• notifyDataSetInvalidated()– No more data available
17
Tuesday, June 1, 2010
![Page 20: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/20.jpg)
Google Confidential
Handling different view typesAdapters
• Built-in item types• getItemViewType
– Type of View for a given position– Used to provide the right convertView
• getViewTypeCount– How many types to expect
18
Tuesday, June 1, 2010
![Page 21: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/21.jpg)
Google Confidential
Handling slow data sourcesAdapters
• Adapter modifications on the UI thread• Fetching data can happen anywhere• Request data on another thread• Commit adapter changes on the UI thread• Call notifyDataSetChanged()
19
Tuesday, June 1, 2010
![Page 22: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/22.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
20
Tuesday, June 1, 2010
![Page 23: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/23.jpg)
Google Confidential
Enabled or disabledItem properties
• Enabled– Item can be selected, clicked
• Disabled– Section dividers/headers within content
21
Tuesday, June 1, 2010
![Page 24: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/24.jpg)
Google Confidential
Enabled or disabledItem properties
22
Tuesday, June 1, 2010
![Page 25: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/25.jpg)
Google Confidential
Choice modeItem properties
• Single choice mode (radio buttons)• Multiple choice mode (checked items)• What is selected?
– Single choice mode• getCheckedItemPosition()
– Multiple choice mode• getCheckedItemPositions()
– Stable IDs make life easier when positions can change • getCheckedItemIds()
23
Tuesday, June 1, 2010
![Page 26: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/26.jpg)
Google Confidential
Choice modeItem properties
24
Tuesday, June 1, 2010
![Page 27: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/27.jpg)
Google Confidential
FocusableItem properties
• setItemsCanFocus(boolean itemsCanFocus)• Do list items focus as a whole? (false)• Or can views within list items have focus? (true)
25
Tuesday, June 1, 2010
![Page 28: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/28.jpg)
Google Confidential
FocusableItem properties
26
Tuesday, June 1, 2010
![Page 29: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/29.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
27
Tuesday, June 1, 2010
![Page 30: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/30.jpg)
Google Confidential
Headers and footers
28
Tuesday, June 1, 2010
![Page 31: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/31.jpg)
Google Confidential
FixedHeaders and footers
29
1 <LinearLayout 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical"> 5 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content"> 9 <!-- ... --> 10 </LinearLayout> 11 12 <ListView 13 android:id="@+id/list" 14 15 android:layout_width="fill_parent" 16 android:layout_height="0dip" 17 android:layout_weight="1.0" /> 18 19 </LinearLayout>
Tuesday, June 1, 2010
![Page 32: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/32.jpg)
Google Confidential
ScrollingHeaders and footers
• ListView.addHeaderView()• ListView.addFooterView()• Must be called before setAdapter()• isSelectable == Adapter.isEnabled()
– Sorry, our bad
30
Tuesday, June 1, 2010
![Page 33: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/33.jpg)
Google Confidential
Wrapped adapterHeaders and footers
31
1 ListView list = ...; 2 list.addHeaderView(myHeader); 3 list.addFooterView(myFooter); 4 list.setAdapter(myAdapter); 5 6 boolean same = list.getAdapter() == myAdapter; 7 // same == false! 8 // getAdapter() returns a 9 // android.widget.HeaderViewListAdapter
Tuesday, June 1, 2010
![Page 34: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/34.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
32
Tuesday, June 1, 2010
![Page 35: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/35.jpg)
Google Confidential
List selectors
• Highlights the selected item• Not shown in touch mode
– There’s no selection in touch mode!• Shown behind list items
– android:drawSelectorOnTop=”true”
33
Tuesday, June 1, 2010
![Page 36: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/36.jpg)
Google Confidential
List selectors
34
Tuesday, June 1, 2010
![Page 37: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/37.jpg)
Google Confidential
List selectors
35
1 <selector> 2 3 <item android:state_window_focused="false" 4 android:drawable="@color/transparent" /> 5 6 <item android:state_focused="true" android:state_enabled="false" 7 android:state_pressed="true" 8 android:drawable="@drawable/list_selector_background_disabled" /> 9 10 <item android:state_focused="true" android:state_enabled="false" 11 android:drawable="@drawable/list_selector_background_disabled" /> 12 13 <item android:state_focused="true" android:state_pressed="true" 14 android:drawable="@drawable/list_selector_background_transition" /> 15 <item android:state_focused="false" android:state_pressed="true" 16 android:drawable="@drawable/list_selector_background_transition" /> 17 18 <item android:state_focused="true" 19 android:drawable="@drawable/list_selector_background_focus" /> 20 21 </selector>
Tuesday, June 1, 2010
![Page 38: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/38.jpg)
Google Confidential
List selectors
• If your items are opaque, use a selector drawable:– convertView.setBackground(R.drawable.selector)
36
1 <selector> 2 <item android:state_selected="true" 3 android:drawable="@color/transparent" /> 4 <item android:state_selected="false" 5 android:drawable="#ff00ff00" /> 6 </selector>
Tuesday, June 1, 2010
![Page 39: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/39.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
37
Tuesday, June 1, 2010
![Page 40: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/40.jpg)
Google Confidential
Transcript and stack from bottomOther features
• android:transcriptMode– Behavior of the list when the content changes– “disabled”, doesn’t scroll– “normal”, scrolls to the bottom if last item is visible– “alwaysScroll”, always scrolls to the bottom
• android:stackFromBottom– Stack items in reverse order– Starts with the last item from the adapter
• Useful for chats– Messaging, Talk, IRC, etc.
38
Tuesday, June 1, 2010
![Page 41: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/41.jpg)
Google Confidential
Transcript and stack from bottomOther features
39
Tuesday, June 1, 2010
![Page 42: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/42.jpg)
Google Confidential
Text filterOther features
• android:textFilterEnabled=”true”• Adapter must implement Filterable
– CursorAdapter, ArrayAdapter, etc.– Implement getFilter()
• Implement the Filter
40
Tuesday, June 1, 2010
![Page 43: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/43.jpg)
Google Confidential
Text filterOther features
41
1 // Filters the content of the adapter on a worker thread2 protected FilterResults performFiltering(CharSequence prefix)3 4 // Displays the results on the UI thread5 protected void publishResults(CharSequence constraint, FilterResults results)
Tuesday, June 1, 2010
![Page 44: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/44.jpg)
Google Confidential
Text filterOther features
42
Tuesday, June 1, 2010
![Page 45: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/45.jpg)
Google Confidential
Agenda
• Virtualization and adapters• Item properties• Headers and footers• List selectors• Other features• Gotchas and don’ts
43
Tuesday, June 1, 2010
![Page 46: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/46.jpg)
Google Confidential
My list turns black?!Gotcha
• Very useful optimization– When scrolling views are cached in bitmaps– Opaque bitmaps to avoid blending
• Solution– android:cacheColorHint=”#00000000”– android:cacheColorHint=”@color/myBackgroundColor”
44
Tuesday, June 1, 2010
![Page 47: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/47.jpg)
Google Confidential
The scrollbar changes size?!Gotcha
• When views have very different heights• Smooth scrollbar needs each item’s height
– Too expensive• Solution
– android:smoothScrollbar=”false”
45
Tuesday, June 1, 2010
![Page 48: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/48.jpg)
Google Confidential
android:layout_height=”wrap_content”Don’t!
• ListView is virtualized, remember?• wrap_content = “as big as my children”
– ListView supports unevenly sized children– Measure thousands of children?
• Android framework cheats– Measures only 3 children– Still expensive
• Wrong result
46
Tuesday, June 1, 2010
![Page 49: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/49.jpg)
Google Confidential
ListView inside a ScrollViewDon’t!
• ScrollView scrolls• ListView scrolls• Who will scroll?
47
Tuesday, June 1, 2010
![Page 50: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/50.jpg)
Google Confidential
Cache views in the adapterDon’t!
• Don’t outsmart ListView– ListView assumes ownership of views
• Complex recycling mechanism– Numerous optimizations– Sometimes we even leave bugs there
• Undead views is the worst case– View both in the recycler and on screen
48
Tuesday, June 1, 2010
![Page 51: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/51.jpg)
Google Confidential
Use a ListView when you don’t need one!Don’t!
• ListView...– Is for repeating, unbounded data– Adds (lots of) complexity
• Will a LinearLayout or ScrollView do the job?
49
Tuesday, June 1, 2010
![Page 52: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/52.jpg)
Tuesday, June 1, 2010
![Page 53: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/53.jpg)
Q&A
View live notes and ask questions about this session on Wave
http://bit.ly/9zozBR
Tuesday, June 1, 2010
![Page 54: Plugin Android World of Listview Android](https://reader034.vdocuments.us/reader034/viewer/2022050801/55258c445503468e6e8b489d/html5/thumbnails/54.jpg)
Tuesday, June 1, 2010