OnTouchListener没有响应

时间:2016-01-29 06:36:20

标签: java android 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 个答案:

答案 0 :(得分:0)

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

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

那是......