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:
-
- Public class Sackofviewsadapter extends Baseadapter {
-
-
-
- private list<view> mviewlist = null;
-
-
-
- /**
-
- * Constructs a view collection with a value of NULL for the size of count, which requires subclasses to override the Newview function
-
- */
-
- Public sackofviewsadapter (int count) {
-
- super ();
-
- Mviewlist = New arraylist<view> (count);
-
- For (int i = 0; i < count; i++) {
-
- Mviewlist.add (null);
-
- }
-
- }
-
-
-
- /**
-
- * 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
-
- */
-
- Public Sackofviewsadapter (list<view> views) {
-
- super ();
-
- this.mviewlist = views;
-
- }
-
-
-
- /**
-
- * Returns the list item corresponding to the location
-
- */
-
- @Override
-
- Public Object GetItem (int position) {
-
- return Mviewlist.get (position);
-
- }
-
-
-
- /**
-
- * Returns the number of items in the list
-
- */
-
- @Override
-
- public int GetCount () {
-
- return mviewlist.size ();
-
- }
-
-
-
- /**
-
- * Number of list item types created by the GetView function
-
- */
-
- @Override
-
- public int Getviewtypecount () {
-
- return GetCount ();
-
- }
-
-
-
- /**
-
- * The view type value created by the GetView function, where the view is located as a type value
-
- */
-
- @Override
-
- public int getitemviewtype (int position) {
-
- return position;
-
- }
-
-
-
- /**
-
- * Returns True if all list items in adapter are clickable and selectable
-
- */
-
- @Override
-
- Public Boolean areallitemsenabled () {
-
- return false;
-
- }
-
-
-
- /**
-
- * Returns TRUE if the list item referred to by position is not a delimiter
-
- */
-
- @Override
-
- Public boolean isenabled (int position) {
-
- return false;
-
- }
-
-
-
- /**
-
- * Returns the view of the list item at the specified position position
-
- */
-
- @Override
-
- Public View GetView (int position, View Convertview, ViewGroup parent) {
-
- View result = Mviewlist.get (position);
-
-
-
- //If view in Mviewlist is null, you need to call the Newview function to create a class that implements the
-
- if (result = = null) {
-
- result = Newview (position, parent);
-
- Mviewlist.set (position, result);
-
- }
-
-
-
- return result;
-
- }
-
-
-
- /**
-
- * Creates a list item at the specified position in the list, which has a subclass implementation
-
- */
-
- protected View newview (int position, ViewGroup parent) {
-
- throw New RuntimeException ("You must override Newview ()!");
-
- }
-
-
-
- /**
-
- * Gets the ID of the list item at the specified location
-
- */
-
- @Override
-
- public long getitemid (int position) {
-
- return position;
-
- }
-
-
-
- /**
-
- * Determine if a specified view exists in the adapter
-
- */
-
- Public Boolean Hasview (View v) {
-
- return Mviewlist.contains (v);
-
- }
-
-
-
- }
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:
-
- Public class Sackofviewsdemo extends Listactivity {
-
-
-
- @Override
-
- public void OnCreate (bundle bundle) {
-
- super.oncreate (bundle);
-
- Requestwindowfeature (Window.feature_no_title);
-
- Setcontentview (R.layout.main);
-
-
-
- arraylist<view> views = new arraylist<view> ();
-
-
-
- Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
-
- //list item 1
-
- View view = Inflater.inflate (r.layout.main_notify, null);
-
- Views.add (view);
-
-
-
- //list item 2
-
- View = Inflater.inflate (r.layout.update_progress_notify, null);
-
- Views.add (view);
-
-
-
- //list Item 3
-
- View = Inflater.inflate (r.layout.notification_battery, null);
-
- Views.add (view);
-
-
-
- //list item 4 (null value, created in the Newview function)
-
- Views.add (null);
-
-
-
- Setlistadapter (new Sillyadapter (views));
-
- }
-
-
-
- class Sillyadapter extends Sackofviewsadapter {
-
- Public Sillyadapter (list<view> views) {
-
- super (views);
-
- }
-
-
-
- protected View newview (int position, ViewGroup parent) {
-
- Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
-
- View view = Inflater.inflate (r.layout.main_notify_red, null);
-
- return view;
-
- }
-
- }
-
- }
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