我要當打開時,它會顯示加息的浮動按鈕與運動結合移動浮動按鈕同樣喜歡「WhatsApp的」應用。並且還在工作點擊事件。浮動按鈕setOnClickListener不被稱爲Android的
我使用默認OnTouch
事件它正在工作,但我不能在浮動按鈕上應用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;
}
});
你得到什麼錯誤 – Abhishek
觸摸事件正在工作,我可以拖放浮動按鈕,但我想改變活動時,點擊它。它不工作。 – Krishna
是你的應用程序崩潰還是什麼? – Abhishek