如何在Android中实现拖放式浮动操作按钮,如flipkart应用程序
答案 0 :(得分:0)
使用setOnTouchListener和监听事件可以实现屏幕周围fab的拖放功能,请尝试下面的代码
fab.setOnTouchListener(new View.OnTouchListener() {
float startX;
float startRawX;
float distanceX;
int lastAction;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
startX = view.getX() - event.getRawX();
startRawX = event.getRawX();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setX(event.getRawX() + startX);
view.setY(event.getRawY() + startX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
distanceX = event.getRawX()-startRawX;
if (Math.abs(distanceX)< 10){
Toast.makeText(getActivity(), "FAB CLICKED", Toast.LENGTH_SHORT).show();
}
break;
case MotionEvent.ACTION_BUTTON_PRESS:
default:
return false;
}
return true;
}
});
答案 1 :(得分:-1)
Draggable Floating Action Buttton:
fabCreate = (FloatingActionButton)view.findViewById(R.id.btnCreate);
fabCreate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
//Toast.makeText(getActivity(), "Clicked!", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_BUTTON_PRESS:
intent = new Intent(getActivity(), PostActivity1.class);
startActivity(intent);
default:
return false;
}
return true;
}
});