可拖动按钮Xamarin.Android

时间:2015-08-21 07:06:47

标签: android button xamarin xamarin.android xamarin-studio

如何在Xamarin.Android中创建可拖动按钮?

我尝试通过创建视图来创建,但它根本不起作用。

任何人都有一些想法或示例代码将不胜感激!

先谢谢。 :)

2 个答案:

答案 0 :(得分:0)

您需要实施OnTouchListener。收听按钮的OnTouch个事件,并在Move事件中相应更新按钮的布局。这是Xamarin的example

答案 1 :(得分:0)

1)扩展View.IOnDragListener的类 2)设置拖动侦听器按钮.SetOnDragListener(this); 3)设置按钮的长按事件

void ButtonLong_Click(object sender, EventArgs e)
    {
        if (utility.IsHomeEditMode&&shortcutList.Count>1)
        {
            View view = (View)sender;

            //get the saved index of the corresponding view
            draggedIndex = Convert.ToInt16(view.GetTag(Resource.String.keyval));

            droppedIndex = -2;

            ClipData.Item item = new ClipData.Item((String)view.GetTag(Resource.String.keyval));
            ClipData clipData = new ClipData((String)view.GetTag(Resource.String.keyval),
                        new String[] { ClipDescription.MimetypeTextPlain }, item);
            view.StartDrag(clipData, new View.DragShadowBuilder(view), null, 0);

            view.Visibility = (ViewStates.Invisible);

        }
    }

实现onDrag方法

       public bool OnDrag(View view, DragEvent e)
    {

        switch (e.Action)
        {
            case DragAction.Started:

                return true;
            case DragAction.Entered:

                return true;
            case DragAction.Exited:

                return true;
            case DragAction.Drop:

                droppedIndex = Convert.ToInt16(view.GetTag(Resource.String.keyval)); ;
                return true;
            case DragAction.Ended:

                return true;

        }
        return false;

    }

更新drop中的视图