我想集成与 "WhatsApp" 应用程序相同的可移动 float 按钮 当打开时,它会显示 远足 float 按钮 和 Action 。还有工作点击事件。
我使用了默认的 OnTouch
事件,它可以正常工作,但我无法在 float 按钮上应用 Click 事件。如何同时应用点击和触摸事件?
提前致谢!
home.xml
<RelativeLayout
android:id="@+id/rl_parent_floating"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp">
<TextView
android:id="@+id/txtCartCount"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@+id/fab"
android:layout_alignTop="@+id/fab"
android:background="@drawable/cart_gold_circle"
android:gravity="center"
android:singleLine="true"
android:text="2"
android:textColor="@android:color/white"
android:textSize="8sp" />
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:src="@drawable/ic_floating_cart"
fab:fab_colorNormal="@color/header_color_red"
fab:fab_colorPressed="@color/colorPrimaryDark"
fab:fab_colorRipple="@color/gold_color" />
</RelativeLayout>
</RelativeLayout>
Home.java
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
mRrootLayout = (ViewGroup) findViewById(R.id.rl_floating_button);
rl_floating_button = (RelativeLayout) findViewById(R.id.rl_floating_button);
rl_parent_floating = (RelativeLayout) findViewById(R.id.rl_parent_floating);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(mActivity, Cart.class);
startActivity(mIntent);
}
});
mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
return true;
}
});
最佳答案
使用 SimpleOnGestureListener & GestureDetector
类在屏幕上单击而不是使用接口(interface) setOnclickListner
因为它可能不适用于 ontouchListner
private class SingleTapDetector extends SimpleOnGestureListener{
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
然后在 onCreate
方法中像这样初始化 GestureDetector
gestureDetector=new GestureDetector(this,new SingleTapDetector());
现在,检查 float 气泡的OnTouchListener
,如果在 float 按钮上检测到单击
代码
mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
// code for single tap or onclick
// pass your intent here
} else {
switch(event.getAction()){
// code for move and drag
// handle your MotionEvents here
}
return true;
}
});
希望对您有所帮助。
关于java - float 按钮 setOnClickListener 不会在 Android 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918882/