lickListener定制的Andr​​oid的FrameLa

lickListener定制的Andr​​oid的FrameLa

本文介绍了OnLongClickListener定制的Andr​​oid的FrameLayout不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的FrameLayout的ImageView的,我想设置LongClickListener但其没有工作,我尝试建立OnTouchListener及其工作完美无瑕,我没有丝毫的想法,为什么它不工作,但下面是我的code code:

 公共类DragImageView扩展的FrameLayout实现View.OnLongClickListener {
ImageView的ivDrag;公共DragImageView(上下文的背景下){
    超级(上下文);
}公共DragImageView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共DragImageView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
}@TargetApi(Build.VERSION_ codeS.HONEYCOMB)
公共无效AddImageView(查看draggableObject,诠释的x,INT Y,INT宽度,高度INT){
    的LayoutParams lpDraggableView =新的LayoutParams(宽度,高度);
    lpDraggableView.gravity = Gravity.TOP;
    lpDraggableView.leftMargin = X;
    lpDraggableView.topMargin = Y;
    如果(draggableObject的instanceof ImageView的){
        this.ivDrag =(ImageView的)draggableObject;
        ivDrag.setLayoutParams(lpDraggableView);
        ivDrag.setClickable(真);
        ivDrag.setLongClickable(真);
        ivDrag.setOnLongClickListener(本);
        this.addView(ivDrag);    }}/ **
 *拖动对象ontouch监听器
 *处理的对象的移动拖放时
 * /
私人View.OnTouchListener OnTouchToDrag =新View.OnTouchListener(){    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        FrameLayout.LayoutParams dragParam =(的LayoutParams)v.getLayoutParams();
        开关(event.getAction())
        {
            案例MotionEvent.ACTION_MOVE:
            {                dragParam.topMargin =(int)的event.getRawY() - (v.getHeight());
                dragParam.leftMargin =(int)的event.getRawX() - (v.getWidth()/ 2);
                v.setLayoutParams(dragParam);
                打破;
            }
            案例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);
                打破;
            }
            案例MotionEvent.ACTION_DOWN:
            {
                dragParam.height = v.getHeight(); //固定在拖放
                dragParam.width = v.getWidth();
                v.setLayoutParams(dragParam);
                打破;
            }
        }
        返回true;
    }};
@覆盖
公共布尔onLongClick(查看视图){
  ivDrag.setOnTouchListener(OnTouchToDrag);
    Toast.makeText(view.getContext(),OnLongClick,Toast.LENGTH_SHORT).show();
    返回false;
}
 }


解决方案

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?

这篇关于OnLongClickListener定制的Andr​​oid的FrameLayout不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 02:26