效果
自定义悬浮按钮
package cn.tsou.floatingactionbuttondemo.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
public class SlidingAroundFrameLayout extends FrameLayout {
private boolean isLeftOut = false;
private boolean isRightOut = false;
public enum TypeEnum {
LEFT,
RIGHT,
}
public SlidingAroundFrameLayout(Context context) {
super(context, null);
}
public SlidingAroundFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public SlidingAroundFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setTranslateAnimation(TypeEnum type) {
TranslateAnimation ta;
if (type == TypeEnum.LEFT) {
if (isLeftOut){
return;
}
isLeftOut = true;
isRightOut = false;
this.setClickable(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
ta.setDuration(500);
ta.setFillAfter(true);
this.startAnimation(ta);
} else if (type == TypeEnum.RIGHT) {
if (isRightOut){
return;
}
isLeftOut = false;
isRightOut = true;
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
ta.setDuration(500);
ta.setFillAfter(true);
this.startAnimation(ta);
this.setClickable(false);
} else {
this.setVisibility(VISIBLE);
}
}
}
ListView滑动显示和隐藏浮动按钮
mListview.setOnTouchListener(onTouchListener);
private boolean isTabShow = true;
private float downY = 0;
// 拖动listview时,如果点击到的地方是item里的一些view,可能出现ACTION_DOWN触发不了的问题。
// 利用isActionDown,当为false时就触发了ACTION_MOVE,第一个action需要当成ACTION_DOWN处理
boolean isActionDown = false;
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isActionDown = true;
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
if (!isActionDown) {
// 当为false时就触发了ACTION_MOVE,第一个action需要当成ACTION_DOWN处理
isActionDown = true;
downY = event.getY();
} else {
float currentY = event.getY();
if (currentY - downY < -20 && isTabShow) {
// 向下,隐藏
isTabShow = false;
mSlidingAround.setTranslateAnimation(SlidingAroundFrameLayout.TypeEnum.RIGHT);
} else if (currentY - downY > 20 && !isTabShow) {
// 向上,显示
isTabShow = true;
mSlidingAround.setTranslateAnimation(SlidingAroundFrameLayout.TypeEnum.LEFT);
}
}
break;
case MotionEvent.ACTION_UP:
isActionDown = false;// isActionDown重置
break;
default:
break;
}
return false;
}
};
RecyclerView滑动显示和隐藏浮动按钮
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mRecyclerview.removeOnScrollListener(myOnScrollListener);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
initData();
}
mRecyclerview.addOnScrollListener(myOnScrollListener);
private RecyclerView.OnScrollListener myOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (Math.abs(dy) < 10) {
return;
}
if (dy > 0) {//向上滑
mSlidingAround.setTranslateAnimation(SlidingAroundFrameLayout.TypeEnum.RIGHT);
} else {//向下滑
mSlidingAround.setTranslateAnimation(SlidingAroundFrameLayout.TypeEnum.LEFT);
}
}
};