【问题标题】:Draggable Button Xamarin.Android可拖动按钮 Xamarin.Android
【发布时间】:2015-08-21 07:06:47
【问题描述】:

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

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

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

提前致谢。 :)

【问题讨论】:

  • 查看此链接blog.xamarin.com/…
  • @Sreeraj :那个博客写得不好。我试图实现它,但失败了。
  • 我没有试过那个例子。但我相信我下面的答案会奏效。我之前已经成功使用过。
  • 好的,让我再试一次。 :)

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


【解决方案1】:

你需要实现OnTouchListener。监听按钮的OnTouch 事件,并在Move 事件中相应地更新按钮的布局。这是 Xamarin 的 example

【讨论】:

  • 我想要一个可拖动的按钮,而不是普通的可拖动视图。它必须能够区分单击和拖动。通过使用Move,我实现了拖动属性。如何实现click属性?
  • 一种简单的方法是设置一个布尔标志 isMoving 并在 Down 时将其设置为 false,在 Move 中将其设置为 True 并在 Up 中检查其值。在 Up 如果它是真的,不要做任何事情。否则写你的点击回调
【解决方案2】:

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;

    }

更新下拉列表中的视图

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 2022-01-09
    • 1970-01-01
    • 2013-11-26
    • 2016-02-27
    • 1970-01-01
    • 2011-10-16
    • 2018-03-04
    • 2020-07-30
    相关资源
    最近更新 更多