AppWidgetHostView can't handle onClick/onLongClick properly

前端 未结 3 577
广开言路
广开言路 2020-12-04 02:36

I am developing a launcher, now I am working with app widgets, I follow the tutorial from here: AppWigetHost tutrial - Leonardo Fischer Everything goes well until I tried a

相关标签:
3条回答
  • 2020-12-04 02:44

    after some days without any answer from SO, I tried to read the source code of Trebuchet-launcher It turns out very simple: extends the AppWidgetHostView and override the method onInterceptTouchEvent() like this source code - I haven't tried it yet, but I guess it will work :).

    Hope this helps anyone like me :)

    0 讨论(0)
  • 2020-12-04 02:45

    I encountered the same problem,I solved it by overwriting the method

    ViewGroup#onInterceptTouchEvent(MotionEvent ev).
    

    See "Managing Touch Events in a ViewGroup" for detail.

    0 讨论(0)
  • 2020-12-04 02:49

    Hungson175's answer was great but it didn't get me there all the way. Since I'm using AppWidgetHost to create the AppWidgetHostView's I needed to extend both AppWidgetHost, and AppWidgetHostView. Luckily this is fairly simple to do and doesn't require too much overriding of default android methods.

    WidgetHost

    public class WidgetHost extends AppWidgetHost {
    
        public WidgetHost(Context context, int hostId) {
            super(context, hostId);
        }
    
        @Override
        protected AppWidgetHostView onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget) {
            // pass back our custom AppWidgetHostView
            return new WidgetView(context);
        }
    }
    

    WidgetView

    public class WidgetView extends AppWidgetHostView {
    
        private OnLongClickListener longClick;
        private long down;
    
        public WidgetView(Context context) {
            super(context);
        }
    
        public WidgetView(Context context, int animationIn, int animationOut) {
            super(context, animationIn, animationOut);
        }
    
        @Override
        public void setOnLongClickListener(OnLongClickListener l) {
            this.longClick = l;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            switch(MotionEventCompat.getActionMasked( ev ) ) {
                case MotionEvent.ACTION_DOWN:
                    down = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_MOVE:
                    boolean upVal = System.currentTimeMillis() - down > 300L;
                    if( upVal ) {
                        longClick.onLongClick( WidgetView.this );
                    }
                    break;
            }
    
            return false;
        }
    }
    

    Hope it helps someone, because dealing with AppWidget's is difficult enough.

    0 讨论(0)
提交回复
热议问题