且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OnLongClickListener定制的Andr​​oid的FrameLayout不工作

更新时间:2023-01-27 18:28:04


  

我想设置LongClickListener但其没有工作


块引用>

您没有收到来自 OnLongClickListener 的回调,因为它没有固定的听众。由于您的类实现 View.OnLongClickListener 并要接收回调在你重写 onLongClick()方法,添加此类本身作为监听器,它会工作。我在构造函数中这样做(选择适当的构造出了三按你的观点的初始化):

 公共DragImageView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    this.setOnLongClickListener(本); //< - 设置这个类的实例,监听
}

虽然我很惊讶你如何得到它与 OnTouchListener 工作。你可能明确添加监听器,对不对?

I have an ImageView in a FrameLayout, I want to setup LongClickListener but its failing to work, I tried setting up OnTouchListener and its working flawless, I do not have the slightest idea as to why its not working but below is my code code:

public class DragImageView extends FrameLayout implements View.OnLongClickListener {
ImageView ivDrag;

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


}



public DragImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}



public DragImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void AddImageView(View draggableObject, int x, int y, int width, int height) {
    LayoutParams lpDraggableView = new LayoutParams(width, height);
    lpDraggableView.gravity = Gravity.TOP;
    lpDraggableView.leftMargin = x;
    lpDraggableView.topMargin = y;
    if(draggableObject instanceof ImageView) {
        this.ivDrag = (ImageView) draggableObject;
        ivDrag.setLayoutParams(lpDraggableView);
        ivDrag.setClickable(true);
        ivDrag.setLongClickable(true);
        ivDrag.setOnLongClickListener(this);
        this.addView(ivDrag);

    }

}

/**
 * Draggable object ontouch listener
 * Handle the movement of the object when dragged and dropped
 */
private View.OnTouchListener OnTouchToDrag =new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        FrameLayout.LayoutParams dragParam = (LayoutParams) v.getLayoutParams();
        switch(event.getAction())
        {
            case MotionEvent.ACTION_MOVE:
            {

                dragParam.topMargin = (int)event.getRawY() - (v.getHeight());
                dragParam.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                v.setLayoutParams(dragParam);
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                dragParam.height = v.getHeight();
                dragParam.width = v.getWidth();
                dragParam.topMargin = (int)event.getRawY() - (v.getHeight());
                dragParam.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                v.setLayoutParams(dragParam);
                break;
            }
            case MotionEvent.ACTION_DOWN:
            {
                dragParam.height = v.getHeight();//fixed on drag and drop
                dragParam.width = v.getWidth();
                v.setLayoutParams(dragParam);
                break;
            }
        }
        return true;
    }

};




@Override
public boolean onLongClick(View view) {
  ivDrag.setOnTouchListener(OnTouchToDrag);
    Toast.makeText(view.getContext(), "OnLongClick", Toast.LENGTH_SHORT).show();
    return false;
}
 }

I want to setup LongClickListener but its failing to work

You are not receiving the callbacks from OnLongClickListener because it has no set listener. Since your class implements View.OnLongClickListener and you want to receive the callback in your overridden onLongClick() method, add this class itself as the listener and it will work. I've done so in the constructor (choose the appropriate constructor out of the three as per your initialization of the view):

public DragImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnLongClickListener(this); // <-- set this class instance as the listener
}

Although I'm surprised how you got it working with OnTouchListener. You probably explicitly added the listener, right?