android - Parent view doesn't get longpress event -


i trying write launcher-like app can add widgets screen.

i using leonardo fischer's tutorial (http://leonardofischer.com/hosting-android-widgets-my-appwidgethost-tutorial/) great.

in order remove widget, user supposed longpress widget , that's running trouble; widgets (whatsapp messagelist, evernote list, instance) allow scroll them. reason, if scroll, android fires longclick event wrongfully removes widget...

my code: (creates widget , set longclicklistener)

public void createwidget(intent data) {     bundle extras = data.getextras();     int appwidgetid = extras.getint(appwidgetmanager.extra_appwidget_id, -1);     appwidgetproviderinfo appwidgetinfo = mappwidgetmanager.getappwidgetinfo(appwidgetid);      final launcherappwidgethostview hostview = (launcherappwidgethostview) mappwidgethost.createview(this, appwidgetid, appwidgetinfo);     hostview.setappwidget(appwidgetid, appwidgetinfo);      // relative layout     //relativelayout.layoutparams lp = new relativelayout()     //mainlayout.addview(hostview, lp);     mainlayout.addview(hostview);      // [commented out] hostview.setonlongclicklistener(new appwidgetlongclicklistener(hostview));  } 

update

countless hours later, think partially understood what's happening, still can't correct behaviour.

according http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/ , need implement onintercepttouchevent in parent container (mainlayout in case) intercept , treat events before reach children (widgets in case).

so googled following code , tried adapt needs:

@override public boolean onintercepttouchevent(motionevent ev) {     // consume touch events ourselves after longpress triggered     //log.i(tag,"onintercept: "+ev.tostring());     if (mhasperformedlongpress) {         log.i(tag,"longpress ok!: "+ev.tostring());         mhasperformedlongpress = false;         return true;     }      // watch longpress events @ level make sure     // users can pick widget     switch (ev.getaction()) {         case motionevent.action_down: {             postcheckforlongclick();             break;         }          case motionevent.action_up:         case motionevent.action_cancel:             mhasperformedlongpress = false;             if (mpendingcheckforlongpress != null) {                 removecallbacks(mpendingcheckforlongpress);             }             break;     }      // otherwise continue letting touch events fall through children     return false; }  class checkforlongpress implements runnable {     private int moriginalwindowattachcount;      public void run() {         log.i(tag,"inside run");         if (getparent()!= null) {             log.i(tag,"getparent:"+getparent().tostring());         }         if ((getparent() != null) && haswindowfocus()                 && (moriginalwindowattachcount == getwindowattachcount())                 && !mhasperformedlongpress) {             if (performlongclick()) { // <-- doesn't work :(                 mhasperformedlongpress = true;             }         }     }      public void rememberwindowattachcount() {         moriginalwindowattachcount = getwindowattachcount();     } }  private void postcheckforlongclick() {     mhasperformedlongpress = false;      if (mpendingcheckforlongpress == null) {         mpendingcheckforlongpress = new checkforlongpress();     }     mpendingcheckforlongpress.rememberwindowattachcount();     postdelayed(mpendingcheckforlongpress, viewconfiguration.getlongpresstimeout()); }  @override public void cancellongpress() {     super.cancellongpress();      mhasperformedlongpress = false;     if (mpendingcheckforlongpress != null) {         removecallbacks(mpendingcheckforlongpress);     } } 

the above code intercept touch events when click widget, logic seems aimed @ intercepting (and direct further treatment) longclicks widgets. need intercept longclick inside parent view.

the trick seems lie @ if (performlongclick()), which, far get, fires longclick event to widget...

... guess question how track longclick inside parent view.

sorry long (and seemingly basic) question on handling android ui events, googled seems convoluted topic..

if seeing event starting scroll being followed event long click, deal setting flag in event handling class tracks when scroll begins , ends, , choose ignore long click if scroll in progress.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -