OnLongClickListener定制的Andr​​oid的FrameLayout不工作 [英] OnLongClickListener not working in custom framelayout Android

查看:129
本文介绍了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;
}
 }


解决方案

  

我想设置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?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆