有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OnTouchListener没有响应

我正在创建一个类似facebook聊天头的小弹出窗口

气泡激发并出现,但ontouchlistener没有响应

ontouch事件完成了我测试它时应该做的事情,但当我集成它时,它不会启动

下面是我用来创建气泡和ontouchlistener布局的代码

public HeadLayer(Context context) {
    super(context);

    mContext = context;
    mFrameLayout = new FrameLayout(mContext);

    addToWindowManager();
}

private void addToWindowManager() {
    final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    myParams.gravity = Gravity.LEFT;

    mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    mWindowManager.addView(mFrameLayout, myParams);

    try{
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Here is the place where you can inject whatever layout you want.
        View view = layoutInflater.inflate(R.layout.head, mFrameLayout);

        //for moving the picture on touch and slide
        headIcon = (ImageView)view.findViewById(R.id.head_icon);

        headIcon.setOnTouchListener(new View.OnTouchListener() {
            WindowManager.LayoutParams paramsT = myParams;
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;
            private long touchStartTime = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        touchStartTime = System.currentTimeMillis();
                        initialX = myParams.x;
                        initialY = myParams.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
                        myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(v, myParams);
                        break;
                }
                return false;
            }
        });
    } catch (Exception e){
        e.printStackTrace();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您好,您首先添加了Framelayout,然后使用LayoutInflator获取其子级。您必须首先初始化所有视图和侦听器,然后将其添加到窗口管理器中

    检查此链接:http://www.piwai.info/chatheads-basics/

    就这样