sách gpt4 ăn đã đi

Android - 可移动/可拖动的 float 操作按钮 (FAB)

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 23:28:33 27 4
mua khóa gpt4 giày nike

tôi đang sử dụng Nút hành động nổi在我的应用程序中。有时,它会与基本内容重叠,所以我想制作它以便用户可以将 FAB 拖到一边。

本身不需要拖放功能。它只需要是可移动的。文档没有提到这一点,但我确信我在其他应用程序中看到过这样的功能。

任何人都可以建议/提供有关如何执行此操作的代码 fragment (最好是 XML 格式)。

câu trả lời hay nhất

dựa trên this answer for another SO question这是我创建的代码。它似乎工作得很好(具有工作点击功能)并且不依赖于 FAB 的父布局或定位......

package com.example;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

private float downRawX, downRawY;
private float dX, dY;

public MovableFloatingActionButton(Context context) {
siêu(bối cảnh);
nhiệt();
}

public MovableFloatingActionButton(Context context, AttributeSet attrs) {
super(bối cảnh, thuộc tính);
nhiệt();
}

public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
nhiệt();
}

riêng tư void init() {
setOnTouchListener(this);
}

@Ghi đè
public boolean onTouch(View view, MotionEvent motionEvent){

ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams();

int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_DOWN) {

downRawX = motionEvent.getRawX();
downRawY = motionEvent.getRawY();
dX = view.getX() - downRawX;
dY = view.getY() - downRawY;

return true; // Consumed

}
else if (action == MotionEvent.ACTION_MOVE) {

int viewWidth = view.getWidth();
int viewHeight = view.getHeight();

View viewParent = (View)view.getParent();
int parentWidth = viewParent.getWidth();
int parentHeight = viewParent.getHeight();

float newX = motionEvent.getRawX() + dX;
newX = Math.max(layoutParams.leftMargin, newX); // Don't allow the FAB past the left hand side of the parent
newX = Math.min(parentWidth - viewWidth - layoutParams.rightMargin, newX); // Don't allow the FAB past the right hand side of the parent

float newY = motionEvent.getRawY() + dY;
newY = Math.max(layoutParams.topMargin, newY); // Don't allow the FAB past the top of the parent
newY = Math.min(parentHeight - viewHeight - layoutParams.bottomMargin, newY); // Don't allow the FAB past the bottom of the parent

view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start();

return true; // Consumed

}
else if (action == MotionEvent.ACTION_UP) {

float upRawX = motionEvent.getRawX();
float upRawY = motionEvent.getRawY();

float upDX = upRawX - downRawX;
float upDY = upRawY - downRawY;

if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
return performClick();
}
else { // A drag
return true; // Consumed
}

}
khác {
return super.onTouchEvent(motionEvent);
}

}

}

这里是 XML...

    <>
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_navigate_next_white_24dp"/>

基本上,您只需将 XML 中的 android.support.design.widget.FloatingActionButton Thay thế bằng com.example.MovableFloatingActionButton

关于Android - 可移动/可拖动的 float 操作按钮 (FAB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46370836/

27 4 0
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress