在这里,我有一个由多个片段共享的视图寻呼机,它们都包含在TABLayout中,如下所示
<relativeLayoutandroID: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.floatingActionbuttonandroID: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由所有片段共享.我想在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类中获取视图,
fab = (floatingActionbutton) findVIEwByID(R.ID.floatingActionbutton);
3)然后为您的视图编写以下代码,
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 – 如何在视图寻呼机上拖动浮动 *** 作按钮全部内容,希望文章能够帮你解决android – 如何在视图寻呼机上拖动浮动 *** 作按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)