android—ListView与RecyclerView滑动显示和隐藏浮动按钮

36 阅读1分钟

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();

【附】相关架构及资料

资料领取

点击这里免费获取Android IOC架构设计等资料

领取获取往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。