android - 如何将 float 操作按钮拖到 View 寻呼机上

标签 android android-fragments android-viewpager floating-action-button onlongclicklistener

在这里,我有一个由多个 fragment 共享的 View 寻呼机,这些 fragment 都包含在一个 TABLayout 中,如下所示

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#006699"
    android:titleTextColor="#FFFFFF"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="#006699"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/order_icon"
android:layout_width="59dp"
android:layout_height="59dp"
android:visibility="visible"
android:layout_gravity="right|bottom"
app:layout_anchor="@id/pager"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/order_icon"
android:layout_alignBottom="@+id/pager"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

FAB 由所有 fragment 共享。我想在 FAB 上添加一个 onLongClicklistener,通过它我可以将按钮拖到布局(包括 viewPager)上。 我已经使用 onTouchListener 和 MotionEvent 对象完成了上述任务。 但是,我无法使用拖放 API 执行此操作(释放拖动的按钮后,按钮消失)。

代码如下:

    orderIcon = (FloatingActionButton)findViewById(R.id.order_icon);
    orderIcon.setTag(IMAGEVIEW_TAG);
    // Sets a long click listener for the ImageView
    orderIcon.setOnLongClickListener(new View.OnLongClickListener() {
     @Override
     public boolean onLongClick(View v) {
         ClipData.Item item = new ClipData.Item((CharSequence) v
                 .getTag());
         String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
         ClipData clipData = ClipData.newPlainText("", "");
         View.DragShadowBuilder myShadow = new View.DragShadowBuilder(view);                    

         // Starts the drag
         v.startDrag(clipData,myShadow,null,0);
         return true;
       }
       });
       orderIcon.setOnDragListener(new View.OnDragListener() {
       @SuppressLint("NewApi")
       @Override
       public boolean onDrag(View v, DragEvent event) {
         switch (event.getAction()) {
             case DragEvent.ACTION_DRAG_STARTED:
                 owner = (ViewGroup) v.getParent();
                 owner.removeView(v);

                 Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
                 break;
             case DragEvent.ACTION_DRAG_ENTERED:
                 Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
                 float x = (int) event.getX();
                 float y = (int) event.getY();
                 break;
             case DragEvent.ACTION_DRAG_EXITED:
                  Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
                  x = (float) event.getX();
                  y = (float) event.getY();
                  v.setX(x);
                  v.setY(y);
                  owner.addView(v);
                  v.setVisibility(View.VISIBLE);
                  break;
             case DragEvent.ACTION_DRAG_LOCATION:
                 Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");

                 break;
             case DragEvent.ACTION_DRAG_ENDED:
                 Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");


                 break;
             case DragEvent.ACTION_DROP:
                 Log.d(msg, "ACTION_DROP event");
                 break;
             default:
                 break;
           }
         return true;
        }
     });

如有任何回应,我们将不胜感激。提前谢谢你。

最佳答案

试试这个,

1) 声明变量。

    float dX;
    float dY;
    int lastAction;

2) 在你的 java 类中获取 View ,

fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);

3) 然后为你的 View 编写如下代码,

fab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        dX = view.getX() - event.getRawX();
                        dY = view.getY() - event.getRawY();
                        lastAction = MotionEvent.ACTION_DOWN;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        view.setY(event.getRawY() + dY);
                        view.setX(event.getRawX() + dX);
                        lastAction = MotionEvent.ACTION_MOVE;
                        break;
                    case MotionEvent.ACTION_UP:
                        if (lastAction == MotionEvent.ACTION_DOWN)
                            Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
                        break;    
                    default:
                        return false;
                }
                return true;
            }
        });

关于android - 如何将 float 操作按钮拖到 View 寻呼机上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35659645/

相关文章:

java - 使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本

android - 如何在标记下移动 map ?

android - 从提示文本中删除搜索 View 图标

Android 后退按钮无法在 fragment 中工作

android - 在 ActionBar 选项卡下的 fragment 中添加/替换 fragment

java - 选项卡布局 : Lag when switching fragments

Android ViewPager,从 fragment 切换到另一个 fragment

java - 如何考虑java中的夏令时计算两个日期之间的全天差

android - (Android)If语句使我的 Activity 崩溃

java - 从 fragment 膨胀 Activity 时出错 : InflateException