<span id="Label3"></p><p><p><span style="font-size:14px">Problem:</span></p></p><p><p><span style="font-size:14px">In android, according to the Android event tree, we know that in <span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; line-height:25.200000762939453px">related events that are triggered when interacting with the application, such as screen keys, touch, swipe</span> , and so on, the interaction events are propagated from top to bottom along the event tree;</span></p></p><p><p><span style="font-size:14px">When the parent control on the upper level of the event tree receives the event, the event is judged to be owned and, if necessary, the event is intercepted, or the child control is propagated down.</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">So we are writing a variety of listener, in the view of various event functions to accept and handle various types of interactive events, is this processing mode is an observer pattern it?</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">problem, put it here First.</span></p></p><p><p><span style="font-size:14px">Anyway</span></p></p><p><p><span style="font-size:14px">Viewer Mode:</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">1. definition:</span></p></p><p><p><span style="font-size:14px">Define a one-to-many dependency between objects so if one object changes state, all its dependents AER notified and Updated automatically.<br>Defines a one-to-many dependency between objects so that when an object changes state, all objects that depend on it are notified and automatically Updated.</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">2. applicable:<br>Consider using the Observer pattern when an object change needs to change other objects at the same time, and he does not know how many objects need to be changed;</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">3. purpose:</span></p></p><p><p><span style="font-size:14px">The work done by the observer pattern is in fact decoupled, so that both sides of the coupling are dependent on and abstracted from each other, and do not depend on the specifics so that neither side changes affect the other;<br></span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">4. Principle:<br><br>The observer pattern conforms to the dependency reversal principle;</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">5. Other:</span></p></p><p><p><span style="font-size:14px">The Observer pattern is the behavior pattern of an object, also called the Publish-subscribe (publish/subscribe) pattern, the Model-view (model/view) mode, the Source-listener (source/listener) mode, or the slave (dependents) Mode.</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px">6, the Simple demo:</span></p></p><p><p><span style="font-size:14px">Demo Business Is: a button object, when the button click, notify two changes to the view;</span></p></p><p><p><span style="font-size:14px">The first is: the Observer:</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package com.example.demo.observer;/** * Abstract Viewer (Observer) Role * @author Qubian * @data June 11, 2015 * @email [email protected] * */public interface Observer {public void Upadteview (String info); Package Com.example.demo.observer;import android.util.log;/** * Specific viewer role * @author Qubian * @data June 11, 2015 * @email [emai L protected] * */public class Observer_view1 implements Observer{@Overridepublic void Upadteview (String Info) {log.i ("upa Dteview "," observer_view1, View 1 changed, The content of the change is: "+info);}} Package Com.example.demo.observer;import android.util.log;/** * Specific viewer role * @author Qubian * @data June 11, 2015 * @email [emai L protected] * */public class observer_view2 implements Observer{@Overridepublic void Upadteview (String Info) {log.i ("upadteview", "observer_view2, View 2 changed, The content of the change is:" +info);}}</pre></pre><br>Subject Role:<p><p></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package Com.example.demo.observer;import java.util.arraylist;import java.util.list;/** * Abstract Theme (Subject) role * @author Qubian * @data June 11, 2015 * @email [email protected] * */public abstract class Subject {private list<observer> List; /** * Add observer, Interface oriented, Abstract programming * @param observer */public void addobserver (observer observer) {if (list==null) {list = new ARR Aylist<observer> ();} List.add (observer);} /** * Notification View changed * @param info */public void Notifydatasetchange (String Info) {for (Observer observer:list) {observer.upadtev Iew (info);}}} Package Com.example.demo.observer;import android.util.log;/** * Specific viewer role * For example, I clicked the button, I hope, the view changes * @author Qubian * @data June 11, 2015 * @email [email protected] * */public class Subject_button extends subject{/** * Click on the button, I hope the view changes */public Voi D Click () {log.i ("subject_button", "click button"); String tag = "101"; Notifydatasetchange (tag);}}</pre></pre><br>Use:<p><p></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">Package com.example.demo.observer;/** * use * @author Qubian * @data June 11, 2015 * @email [email protected] * */public C Lass useobserver {public void use () {//business is like this: I clicked the button and I want the view to be bound on this button to Change//first there are two views of the Observer: Observer obj1 = new Observer_view1 (); Observer obj2 = new Observer_view2 ();//button Subject_button sub = new Subject_button (); Sub.addobserver (obj1); sub.addobserver (obj2);//click the button view to change sub. Click ();}}</pre></pre><br>7. Java library:<br><br>Inside the Java.util library in java, a observable class and a observer interface are provided, which form the Java language support for the Observer Pattern.<p><p></p></p><p><p><span style="font-size:14px">Use relatively simple, the concrete observation object implements <span style="font-size:14px">observer interface, The Observer inherits <span style="font-size:14px">observable class, Can. </span></span></span></p></p><p><p><span style="font-size:14px">Relatively simple, no longer explained;</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java">/* * Licensed to the Apache software Foundation (ASF) under one or more * Contributor license Agreements. See the NOTICE file distributed with * This work for additional information regarding copyright Ownership. * The ASF licenses this file to you under the Apache License, Version 2.0 * (the "License"); You are not a use of this file except in compliance with * the LICENSE. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by Applica BLE law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR Implied. * See the License for the specific language governing permissions and * Limitations under the License. */package java.util;/** * {@code Observer} is the interface to being implemented by objects that * receive notification of UP Dates on a {@code Observable} object. * * @see Observable */public Interface ObserVer {/** * This method is called if the specified {@code Observable} object ' s * {@code notifyobservers} method is called (because, {@code Observable} * object has been Updated. * * @param observable * The {@link observable} object. * @param data * The data passed to {@link observable#notifyobservers (Object)}. */void update (Observable Observable, Object data);} Package Android.database;import java.util.arraylist;/** * Provides methods for registering or unregistering arbitrary obs Ervers in an {@link ArrayList}. * * This abstract class was intended to being subclassed and specialized to maintain * a registry of observers of specific Typ Es and dispatch notifications to Them. * * @param T The Observer Type. */public abstract class observable<t> {/** * The list of Observers. An observer can is in the list at most * once and would never be null. */protected final arraylist<t> mobservers = new ArraYlist<t> (); /** * ADDS a observer to the List. The observer cannot is null and it must not already * is Registered. * @param observer The Observer to register * @throws ILLEGALARGUMENTEXCEPTION The Observer is NULL * @throws Illeg Alstateexception The Observer is already registered */public void Registerobserver (T observer) {if (observ ER = = 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); }}/** * Removes a previously registered Observer. The observer must not being null and it * must already has been registered. * @param observer The Observer to unregister * @throws ILLEGALARGUMENTEXCEPTION The Observer is NULL * @throws ILL Egalstateexception The observer is not yet registered */public void Unregisterobserver (T observer) {if (observer = = Null) { Throw new IllegalArgumentException ("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); }}/** * Remove all registered Observers. */public void UnregisterAll () {synchronized (mobservers) {mobservers.clear (); } }}</pre><br>8, the use of android:<p><p></p></p><p><p><span style="font-size:14px">Return to, above the question;</span></p></p><p><p><span style="font-size:14px">obviously, the above-mentioned Android event Tree transmission, Interactive event monitoring, we wrote the listener callback, the whole listening part is an observer pattern.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"><br></span></span></p></p><p><p><span style="font-size:14px">in <span style="font-size:14px">our most familiar click event,</span> when initializing, we set the monitor for the View:</span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> /** * Register A callback to being invoked when the this view is Clicked. If This view was not * clickable, it becomes clickable. * * @param l The callback that would run * * @see #setClickable (boolean) */public void setonclicklist Ener (onclicklistener l) { if (!isclickable ()) { setclickable (true); } Getlistenerinfo (). Monclicklistener = l; }</pre></pre><br><p><p></p></p><p><p><span style="font-size:14px"><span style="font-family:Monaco">Description of the Onclicklistener interface:</span></span></p></p><p><p><span style="font-size:14px"><span style="font-family:Monaco"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"> /** * Interface definition for a callback to being invoked when a view is Clicked. */public Interface Onclicklistener { /** * Called if a view has been Clicked. * * @param v The view that is Clicked. * /void OnClick (View v); }</pre></pre><br><p><p></p></p><p><p><span style="font-family:Monaco; font-size:14px">Onclicklistener is in the View view inner class Listenerinfo:</span></p></p><pre name="code" class="java">Static class Listenerinfo {protected Onfocuschangelistener monfocuschangelistener; Private Arraylist<onlayoutchangelistener> monlayoutchangelisteners; Private Copyonwritearraylist<onattachstatechangelistener> monattachstatechangelisteners; /** * Listener used to dispatch click Events. * This field should was made private, so it's hidden from the SDK. * {@hide} */public Onclicklistener monclicklistener; Protected Onlongclicklistener monlongclicklistener; Protected Oncreatecontextmenulistener moncreatecontextmenulistener; Private Onkeylistener monkeylistener; Private Ontouchlistener montouchlistener; Private Onhoverlistener monhoverlistener; Private Ongenericmotionlistener mongenericmotionlistener; Private Ondraglistener mondraglistener; Private Onsystemuivisibilitychangelistener monsystemuivisibilitychangelistener; }</pre><br><p><p></p></p><p><p><span style="font-family:Monaco; font-size:14px">Call:</span></p></p><p><p><span style="font-family:Monaco; font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> public boolean PerformClick () { sendaccessibilityevent (accessibilityevent.type_view_clicked); Listenerinfo li = mlistenerinfo; If (li! = null && li.monclicklistener! = Null) { playsoundeffect (soundeffectconstants.click); Li.mOnClickListener.onClick (this); return true; } return false; } public boolean Callonclick () { listenerinfo li = mlistenerinfo; If (li! = null && li.monclicklistener! = Null) { li.mOnClickListener.onClick (this); return true; } return false; }</pre></pre><br>As for the PerformClick call, it is in the view of the monitoring ontouchevent, etc. have specific embodiment:<p><p></p></p><p><p> <span style="font-family:monaco; font-size:14px"> </span> </p> </p><pre name="code" class="java"> public boolean ontouchevent (motionevent event) {if ((viewflags & clickable) = = Clickable | | (viewflags & long_clickable) = = Long_clickable) {switch (event.getaction ()) {case motione Vent. Action_up:boolean prepressed = (mprivateflags & pflag_prepressed)! = 0; If ((mprivateflags & pflag_pressed)! = 0 | | Prepressed) {//take FOCUS If we don't have it ALR Eady and we should In//touch Mode. Boolean focustaken = false; If (isfocusable () && isfocusableintouchmode () &&!isfocused ()) {focustaken = r Equestfocus (); } If (prepressed) {//the button is being released before we actually showed it as Pressed. Make it show the pressed State now (before scheduling the Click) to ensure//the user sees it. Setpressed (true); If (!mhasperformedlongpress) {//this was a tap, so remove the Longpre SS Check Removelongpresscallback (); Only perform take click Actions if we were in the pressed state if (!focustaken) { Use a Runnable and post this rather than Calling//performclick D Irectly. This lets other visual state//of the view update before click Actions Start. if (mperformclick = = Null) {mperformclick = new PerformClick (); } if (!post (mperformclick)) { PerformClick (); }}} if (munsetpressedstate = = Null) { Munsetpressedstate = new Unsetpressedstate (); } If (prepressed) {postdelayed (munsetpressedstate, Viewconfiguration.getpressedstateduration ()); } else if (!post (munsetpressedstate)) {//if the post failed, unpress right now Munsetpressedstate.run (); } removetapcallback (); } break; Case MotionEvent.ACTION_DOWN:break; Case MotionEvent.ACTION_CANCEL:case Motionevent.action_move: Break } return true; } return false; }</pre><p><p></p></p><p><p><br></p></p><p><p>Android Design Mode--viewer mode</p></p></span>
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