Android Open source code interpretation-based on the Sackofviewadapter class to implement a similar status notification bar layout

Source: Internet
Author: User



In general, the ListView list items will be the same layout, but the content of the fill is different, in this case, Android provides convertview to help us cache list items, to achieve the purpose of recycling, Developers will also use the Viewholder mode to optimize the ListView. However, in some cases, the ListView list item layout is not the same, or even very different, it is not easy to use the same layout resource file to represent different types of list items, but must be treated differently. Typical situations such as the Android status notification bar, as shown in.






 sing it, Flash push these three apps have very different layouts in the status notification bar, so we want to use the same adapter to represent the various list items, then we need to use the container in adapter to contain the different view. This defines the Sackofviewadapter class, inherits the Baseadapter, defines the container variable mviewlist in the class to store the view of the different list items in the ListView, and defines two constructors, one specifying the size of the container in the parameter. And fills a null value, and one passes directly to the collection of all the view in the ListView list item. The code is as follows:









The key code for the Sackofviewadapter class is in the Newview function and the GetView function, where the Newview function is used to assign a value to a null element in the container variable mviewlist, typically implemented by a subclass of Sackofviewadapter. The GetView function overrides the Baseadapter class, which is the key function that adapter returns an instance of the view class, as shown in the following code:












The complete code for the Sackofviewadapter class is as follows:


  1. Public class Sackofviewsadapter extends Baseadapter {
  2. private list<view> mviewlist = null;
  3. /** 
  4. * Constructs a view collection with a value of NULL for the size of count, which requires subclasses to override the Newview function
  5. */
  6. Public sackofviewsadapter (int count) {
  7. super ();
  8. Mviewlist = New arraylist<view> (count);
  9. For (int i = 0; i < count; i++) {
  10. Mviewlist.add (null);
  11. }
  12. }
  13. /** 
  14. * Assign values directly to the container by the incoming view collection, and if there is a null value view in the View collection, the subclass must implement the Newview function
  15. */
  16. Public Sackofviewsadapter (list<view> views) {
  17. super ();
  18. this.mviewlist = views;
  19. }
  20. /** 
  21. * Returns the list item corresponding to the location
  22. */
  23. @Override
  24. Public Object GetItem (int position) {
  25. return Mviewlist.get (position);
  26. }
  27. /** 
  28. * Returns the number of items in the list
  29. */
  30. @Override
  31. public int GetCount () {
  32. return mviewlist.size ();
  33. }
  34. /** 
  35. * Number of list item types created by the GetView function
  36. */
  37. @Override
  38. public int Getviewtypecount () {
  39. return GetCount ();
  40. }
  41. /** 
  42. * The view type value created by the GetView function, where the view is located as a type value
  43. */
  44. @Override
  45. public int getitemviewtype (int position) {
  46. return position;
  47. }
  48. /** 
  49. * Returns True if all list items in adapter are clickable and selectable
  50. */
  51. @Override
  52. Public Boolean areallitemsenabled () {
  53. return false;
  54. }
  55. /** 
  56. * Returns TRUE if the list item referred to by position is not a delimiter
  57. */
  58. @Override
  59. Public boolean isenabled (int position) {
  60. return false;
  61. }
  62. /** 
  63. * Returns the view of the list item at the specified position position
  64. */
  65. @Override
  66. Public View GetView (int position, View Convertview, ViewGroup parent) {
  67. View result = Mviewlist.get (position);
  68. //If view in Mviewlist is null, you need to call the Newview function to create a class that implements the
  69. if (result = = null) {
  70. result = Newview (position, parent);
  71. Mviewlist.set (position, result);
  72. }
  73. return result;
  74. }
  75. /** 
  76. * Creates a list item at the specified position in the list, which has a subclass implementation
  77. */
  78. protected View newview (int position, ViewGroup parent) {
  79. throw New RuntimeException ("You must override Newview ()!");
  80. }
  81. /** 
  82. * Gets the ID of the list item at the specified location
  83. */
  84. @Override
  85. public long getitemid (int position) {
  86. return position;
  87. }
  88. /** 
  89. * Determine if a specified view exists in the adapter
  90. */
  91. Public Boolean Hasview (View v) {
  92. return Mviewlist.contains (v);
  93. }
  94. }





Finally, an example is used to illustrate the use of the Sackofviewadapter class, in which we define 4 different layout list items, one of which is a null value, and the Sackofviewadapter subclass overrides the Newview function implementation assignment. The other three is not a null value. The layout files for these four view are Main_notify.xml, Update_progress_notify.xml, Notification_battery.xml, and Main_notify_, respectively. Red.xml, these resources are borrowed as shown in the example effect:









The code for the instance is as follows:





  1. Public class Sackofviewsdemo extends Listactivity {
  2. @Override
  3. public void OnCreate (bundle bundle) {
  4. super.oncreate (bundle);
  5. Requestwindowfeature (Window.feature_no_title);
  6. Setcontentview (R.layout.main);
  7. arraylist<view> views = new arraylist<view> ();
  8. Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
  9. //list item 1
  10. View view = Inflater.inflate (r.layout.main_notify, null);
  11. Views.add (view);
  12. //list item 2
  13. View = Inflater.inflate (r.layout.update_progress_notify, null);
  14. Views.add (view);
  15. //list Item 3
  16. View = Inflater.inflate (r.layout.notification_battery, null);
  17. Views.add (view);
  18. //list item 4 (null value, created in the Newview function)
  19. Views.add (null);
  20. Setlistadapter (new Sillyadapter (views));
  21. }
  22. class Sillyadapter extends Sackofviewsadapter {
  23. Public Sillyadapter (list<view> views) {
  24. super (views);
  25. }
  26. protected View newview (int position, ViewGroup parent) {
  27. Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
  28. View view = Inflater.inflate (r.layout.main_notify_red, null);
  29. return view;
  30. }
  31. }
  32. }





The Sackofviewadapter class reference in this paper is from https://github.com/commonsguy/cwac-sacklist



The source code of this article see: http://download.csdn.net/detail/ace1985/4575749



Android Open source code interpretation-based on the Sackofviewadapter class to implement a similar status notification bar layout


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.