After rewriting View of onTouchEvent, set the click event onClick () method ineffective problem solving

Summary: After a custom view Android rewrite onTouchEvent method, also want to respond to onClick method into effect, it is best to call it performClick () method up in the event.

helpLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        helpLayout.setSelected(true);
                        helpLayout.setPressed(true);
                        settingLayout.setSelected(false);
                        aboutLayout.setSelected(false);
                        break;
                    case MotionEvent.ACTION_UP:
                        helpLayout.setPressed(false);
                        //添加此方法,使onclick事件生效
                        helpLayout.performClick();
                        break;
                }
                return false;
            }
        });

For more details, see: https: //www.jianshu.com/p/7d1e773d9955

Published 15 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/u013769274/article/details/102695431