问题描述
我正在寻找一种方法,如何像在 todoist 应用程序中一样在列表中拖动 fab:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.jetbrains.annotations.NotNull;
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;
private FabMoveListener moveListener;
private OnTouchListener l;
public void setMoveListener(FabMoveListener moveListener) {
this.moveListener = moveListener;
}
public MovableFloatingActionButton(Context context) {
super(context);
init();
}
public MovableFloatingActionButton(Context context,AttributeSet attrs) {
super(context,attrs);
init();
}
public MovableFloatingActionButton(Context context,AttributeSet attrs,int defStyleAttr) {
super(context,attrs,defStyleAttr);
init();
}
private void init() {
setonTouchListener(this);
}
@Override
public boolean onTouch(@NotNull View view,@NotNull MotionEvent motionEvent) {
l.onTouch(view,motionEvent);
moveListener.onEvent(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
}
} else {
return super.onTouchEvent(motionEvent);
}
}
public interface FabMoveListener {
void onEvent(MotionEvent event);
}
}
我假设 itemtouchhelper 也用于此目的,但我仍然没有实现类似行为的运气。 实际上,我需要在 itemtouchhelper 中开始拖动并同时拖动一个新的 viewholder 和一个按钮,但是我该怎么做?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)