如何将浮动动作按钮拖动到视图寻呼机上 [英] How to drag a floating action button over a view pager
问题描述
在这里,我有一个由多个Fragment共享的视图分页器,所有这些片段都包含在TABLayout中,如下所示
Here, I have a view pager that is shared by multiple Fragments which all are contained in a TABLayout as shown below
<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由所有片段共享.我想在FAB上添加onLongClicklistener,可以通过它在布局(包括viewPager)上拖动按钮. 我已经使用onTouchListener和MotionEvent对象完成了上述任务. 但是,我无法使用拖放API(在释放拖动的按钮后,该按钮消失了).
The FAB is shared by all the fragments. I want to add a onLongClicklistener on the FAB by which I can drag the button over the layout(including the viewPager). I have done the above task using onTouchListener and the MotionEvent object. However, I am unable to do so using the drag and drop API(after releasing the dragged button, the button disappears).
这是代码:
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;
}
});
任何回应将不胜感激.预先谢谢你.
Any response would be highly appreciated. Thank you in advance.
推荐答案
尝试一下,
1)声明变量.
float dX;
float dY;
int lastAction;
2)在您的Java类中获取视图,
2) Get the view in your java class,
fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
3)然后为您的视图编写以下代码,
3) Then write the following code for your 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;
}
});
这篇关于如何将浮动动作按钮拖动到视图寻呼机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!