拖放浮动操作菜单不起作用

时间:2017-04-19 20:38:58

标签: java android

我刚刚创建了一个浮动操作菜单我需要这个浮动操作菜单Draggable我已经创建了一个拖放视图类并创建了一个浮动操作菜单对象但它仍然不起作用这里的拖放类

package niko.dragdrop.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * @author Niko
 * View Container to place the view wanted to be dragged
 */
public class DragDropView extends FrameLayout {

    /**
     * Default Constructor
     * @param context
     */
    public DragDropView(Context context) {
        super(context);
    }

    /**
     * Default Constructor
     * @param context
     * @param attrs
     */
    public DragDropView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Default Constructor
     * @param context
     * @param attrs
     * @param defStyle
     */
    public DragDropView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /** Adding draggable object to the dragView
     * @param draggableView - object to be dragged
     * @param - x horizontal position of the view
     * @param - y vertical position of the view
     * @param - width width of the view
     * @param - height height of the view
     */
    public void AddDraggableView(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) {
            ImageView ivDrag = (ImageView) draggableObject;
            ivDrag.setLayoutParams(lpDraggableView);
            ivDrag.setOnTouchListener(OnTouchToDrag);
            this.addView(ivDrag);
        } 
        //TODO implement to do other type of view
//      else if(draggableObject instanceof TextView) {
//          TextView tvDrag = (TextView) draggableObject;
//          tvDrag.setLayoutParams(lpDraggableView);
//          tvDrag.setOnTouchListener(OnTouchToDrag);
//          this.addView(tvDrag);
//      } 
//      else if(draggableObject instanceof Button) {
//          Button btnDrag = (Button) draggableObject;
//          btnDrag.setLayoutParams(lpDraggableView);
//          btnDrag.setOnTouchListener(OnTouchToDrag);
//          this.addView(btnDrag);
//      }

    }

    /**
     * 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 = 150;
                //  dragParam.width = 150;
                    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 = 100;
                //  dragParam.width = 100;
                        dragParam.height = v.getHeight();
                                dragParam.width = v.getWidth();
                    v.setLayoutParams(dragParam);
                    break;
                }
            }
            return true;
        }

    };

}

这是浮动操作菜单属性

FloatingActionMenu mainFM;

private Context context;
    mainFM = (FloatingActionMenu) findViewById(R.id.mainFAB);
    context = this;
    FrameLayout llContainerMain = (FrameLayout) findViewById(R.id.content_frame);
    DragDropView dragDropView = new DragDropView(context);
    dragDropView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    FloatingActionMenu fam = new FloatingActionMenu(context);
    dragDropView.AddDraggableView(mainFM, 50, 50, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    llContainerMain.addView(dragDropView);

如果有人能帮我解决这个案子,我会非常感谢抱歉如果有任何事情不清楚并抱歉我的英语不好

0 个答案:

没有答案