如何解决如何在浮动操作按钮上创建自定义点击侦听器
如何在FAB上创建自定义点击侦听器或如何使用返回值performClick()
?
我一直在尝试让android浮动按钮来响应点击,但是在我的自定义click事件上不起作用,我还尝试了在初始化类时没有运气的情况下传递自定义点击。请问我想念什么?
import com.google.android.material.floatingactionbutton.FloatingActionButton;
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;
CustomClickListener customClickListener;
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(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
if (customClickListener != null) {
customClickListener.onClick(view);
}
return false;
//return performClick();
}
else { // A drag
return true; // Consumed
}
}
else {
return super.onTouchEvent(motionEvent);
}
}
public void setCustomClickListener(CustomClickListener customClickListener) {
this.customClickListener = customClickListener;
}
public interface CustomClickListener {
void onClick(View view);
}
}
这是我在主要活动上使用课程的方式
MovableFloatingActionButton fl = new MovableFloatingActionButton(this);
fl.setCustomClickListener(new MovableFloatingActionButton.CustomClickListener() {
@Override
public void onClick(View view) {
Log.e("FAB","Float Clicked");
}
});
解决方法
在这种情况下,执行点击将不起作用,因为您已经创建了自定义的点击侦听器,因此无论何时使用performClick()
,都会执行setOnClickListener
的回调。但是您正在使用CustomClickListener
。
因此,作为解决方案,您必须创建其他方法来对CustomClickListener
进行点击。
在您的课程MovableFloatingActionButton
内,只需添加以下方法:
public void customClick() {
if (customClickListener != null) {
customClickListener.onClick(this);
}
}
然后使用此方法执行代码。
要考虑的另一件事是,您具有以下代码。
MovableFloatingActionButton fl = new MovableFloatingActionButton(this);
如果您正在动态使用此“浮动”按钮并将其动态添加到视图中,而无需进行任何更改。
但是,如果您正在使用内部xml文件并以Java文件进行访问,则必须像这样使用:
MovableFloatingActionButton fl= findViewById(R.id.mov);
,随后您可以使用fl.customClick()
执行自定义点击侦听器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。