Adapter (Adapter) Details: http://blog.csdn.net/jjwwmlp456/article/details/39893723
The Observer (Observer) Details: http://blog.csdn.net/jjwwmlp456/article/details/39967021
Adapterview system
adapter is used in Adapterview
Adapter system
Baseadapter realizes the Spinneradapter, ListAdapter
In this form, the adapter mode is used, and here is the interface adapter
Viewer (Observer) mode
Methods for registering and deserializing datasetobserver are defined in adapter
Public interface Adapter { void Registerdatasetobserver (Datasetobserver observer); VOID Unregisterdatasetobserver (Datasetobserver observer); ...}
Public abstract class Datasetobserver {public void onChanged () { //does nothing } public void Oninvalidat Ed () { //Do Nothing }}
public abstract class Baseadapter implements ListAdapter, Spinneradapter {private final Datasetobservable mdatasetobservable = new datasetobservable (); public void Registerdatasetobserver (Datasetobserver observer) {MDATASETOBSERVABLE.REGISTEROBSERVER (Observer); } public void Unregisterdatasetobserver (Datasetobserver observer) {Mdatasetobservable.unregisterobserver (observ ER); } public void Notifydatasetchanged () {mdatasetobservable.notifychanged (); } public void notifydatasetinvalidated () {mdatasetobservable.notifyinvalidated (); } ...}
public class Datasetobservable extends observable<datasetobserver> {public void n Otifychanged () {synchronized (mobservers) {for (int i = Mobservers.size ()-1; I >= 0; i--) { Mobservers.get (i). onChanged (); }}} public void notifyinvalidated () {synchronized (mobservers) {for (int i = Mobservers . Size ()-1; I >= 0; i--) {mobservers.get (i). oninvalidated (); } } }}
Public abstract class Observable<t> {protected final arraylist<t> mobservers = new arraylist<t> (); public void Registerobserver (T observer) {if (Observer = = null) {throw new IllegalArgumentException ( "The observer is null."); } synchronized (Mobservers) {if (Mobservers.contains (Observer)) {throw new illegalstatee Xception ("Observer" + Observer + "is already registered."); MOBSERVERS.ADD (Observer); }} public void Unregisterobserver (T observer) {if (Observer = = null) {throw new illegalargument Exception ("The Observer is null."); } synchronized (mobservers) {int index = MOBSERVERS.INDEXOF (Observer); if (index = =-1) {throw new IllegalStateException ("Observer" + Observer + "is not registered."); } mobservers.remove (index); }} public void UnregisterAll () { Synchronized (mobservers) {mobservers.clear (); } }}
Datasetobserver Observer (observation data set: Change onchanged, invalid oninvalidated)
Observable processing observers (add, remove, register, anti-registration)
Datasetobservable extends Observable<datasetobserver> added two ways to change and invalidate notification data
In the Adapterview
Class Adapterdatasetobserver extends Datasetobserver {private parcelable minstancestate = null; @Override public void onChanged () {mdatachanged = true; Molditemcount = Mitemcount; Mitemcount = Getadapter (). GetCount (); Detect the case where a cursor is previously invalidated have//been repopulated with new data. if (AdapterView.this.getAdapter (). Hasstableids () && minstancestate! = null && mOl Ditemcount = = 0 && mitemcount > 0) {AdapterView.this.onRestoreInstanceState (minstancestate); Minstancestate = null; } else {remembersyncstate (); } checkfocus (); Requestlayout (); } @Override public void oninvalidated () {mdatachanged = true; if (AdapterView.this.getAdapter (). Hasstableids ()) {//Remember the curRent state for the case where we hosting activity is being//stopped and later restarted m Instancestate = AdapterView.this.onSaveInstanceState (); }//Data is invalid so we should reset our state molditemcount = Mitemcount; Mitemcount = 0; Mselectedposition = invalid_position; Mselectedrowid = invalid_row_id; Mnextselectedposition = invalid_position; Mnextselectedrowid = invalid_row_id; Mneedsync = false; Checkfocus (); Requestlayout (); } public void Clearsavedstate () {minstancestate = null; } }
Custom Adapterdatasetobserver, called Requestlayout () in both implementations, to reset the layout
The process by which the entire observation data changes resulting in an interface change:
1. Customize the Adapterdatasetobserver in the Adapterview system and register (i.e. add observer to observable).
2. After calling Adapter.notifydatasetchanged (), the internal execution of observable's notifychanged (),
The onchanged () of each observer will be executed, and the effect of the update interface is achieved.
notifydatasetinvalidated
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Adapter (Adapter), viewer (Observer) mode