1

I have an imageView with some listeners, such as onClickListener and onLongClickListener. The onLongClickListener responds by adding an onTouchListener to the same view, to listen for motion events. The purpose of this is that when user long clicks on the button, he can then resize it.

The imageView resizing works (not very well, but enough for testing), but, after resizing, all the listeners attached to the view only respond to touches in the area covered by the original dimensions of the imageView. So if user stretches the button downwards to double its height, only the top half will respond to click and touch events.

So do I have to either;

  • detach and re-attach all of the listeners?

  • update the listeners somehow?

Here is my code

sendButton = (ImageView) findViewById(R.id.sendButton);
    sendButton.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            Log.d("long click", "long click");
            v.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    final int action = event.getAction();
                    int currentY = 0;
                    switch (action & MotionEvent.ACTION_MASK) {

                    case MotionEvent.ACTION_DOWN:
                        currentY = (int) event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        if (event.getY() > currentY) {
                            Log.d("y dir", String.valueOf(event.getY()));
                            ViewGroup.LayoutParams params = buttonSpace
                                    .getLayoutParams();
                            params.height++;
                            buttonSpace.setLayoutParams(params);
                        } else if (event.getY() < currentY) {
                            Log.d("y dir", "up");
                            ViewGroup.LayoutParams params = buttonSpace
                                    .getLayoutParams();
                            params.height--;
                            buttonSpace.setLayoutParams(params);
                        }
                        currentY = (int) event.getY();
                        break;

                    }
                    return false;
                }
            });
            return true;
        }
    });

    sendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendButtonPressed();
        }
    });
4

2 回答 2

1

Just a tip, you can just switch on event.getAction(), there's no need to use the bitmask if you're not handling multiple pointers.

As far as your problem, you should be requesting a layout after changing the layout params. Remove the setLayoutParams() calls (you're already modifying the params that are attached the view) and replace them with buttonSpace.requestLayout().

于 2013-03-08T17:17:05.053 回答
0

So the problem actually turned out to be the fact that my listener was attached to the wrong element. I was attaching it to sendButton (which is a child of buttonSpace) but I was only resizing buttonSpace.

Here is the refactored code, with other improvements suggested here.

buttonSpace = (RelativeLayout) findViewById(R.id.button_space);
buttonSpace.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            v.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    final int action = event.getAction();
                    ViewGroup.LayoutParams params = buttonSpace
                            .getLayoutParams();
                    switch (action) {

                    case MotionEvent.ACTION_DOWN:
                        oldY = (int) event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        int newY = (int)event.getY();

                        int deltaY = 0;
                        if(oldY > 0){
                            deltaY = oldY - newY;
                        }
                        oldY = newY;
                        params.height -= deltaY;
                        buttonSpace.requestLayout();
                        break;
                    }
                    return false;
                }
            });
            return true;
        }
    });

    buttonSpace.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendButtonPressed();
        }
    });
于 2013-03-10T15:24:24.513 回答