RecyclerView滑動監聽實現button消失與展現動畫
阿新 • • 發佈:2019-01-08
需求說明:
由於介面大小限制,我們寫完listview或者recyclerView展現圖片後已經佔據了整個螢幕,螢幕下方還有個button,我們需要在滑動圖片的時候button隱藏,出現圖片的時候展現button。
需求分析:
一、給receycleview新增滑動監聽事件
二、給button新增動畫效果
需求實現:
一、我們要實現滑動監聽
//定義自動隱藏監聽內部類,實現recycleView滑動監聽 public AutoHideListener(Activity activity, View view, Animation hide, int hideDuration, Animation show, int showDuration) { _ref = new WeakReference<>(activity); _view = view; _hide = hide; _hideDuration = hideDuration; _show = show; _showDuration = showDuration; } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { final Activity activity = _ref.get(); if (activity == null) return; switch (newState) { case RecyclerView.SCROLL_STATE_IDLE: doOnIdle(activity); break; case RecyclerView.SCROLL_STATE_DRAGGING : case RecyclerView.SCROLL_STATE_SETTLING: LogUtil.d("SCROLL_STATE_SETTLING"); doOnScroll(activity); break; } //_onScrollChange = false; } private void doOnScroll(final Activity activity) { if (_state != Hide) { new Thread() { @Override public void run() { super.run(); if (_onScrollChange) { activity.runOnUiThread(new Runnable() { @Override public void run() { _state = Hide; _view.setClickable(false); _view.startAnimation(_hide); } }); } } }.start(); } } private void doOnIdle(final Activity activity) { _onScrollChange = false; if (_state != Show) { if (_idleThread != null) { synchronized (this) { _idleTime = DefaultIdleTime; } } else { _idleTime = DefaultIdleTime; _idleThread = new Thread() { @Override public void run() { super.run(); synchronized (this) { while (_idleTime > 0) { _idleTime -= 100; SystemClock.sleep(100); } } activity.runOnUiThread(new Runnable() { @Override public void run() { _view.setClickable(true); _view.startAnimation(_show); } }); SystemClock.sleep(_showDuration); _state = Show; _idleThread = null; } }; _idleThread.start(); } } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); _onScrollChange = true; } }
二、實現監聽方法
private static void setViewAutoHide(final Activity activity, final View view, final RecyclerView list) { view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressLint("NewApi") @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); int height = view.getMeasuredHeight() + 2; Log.i("TAGAAA", "height=" + height); final TranslateAnimation hide = new TranslateAnimation(0, 0, 0, height); final int hideDuration = 250; hide.setDuration(hideDuration); AccelerateDecelerateInterpolator i = new AccelerateDecelerateInterpolator(); hide.setInterpolator(i); hide.setFillAfter(true); final TranslateAnimation show = new TranslateAnimation(0, 0, height, 0); final int showDuration = 250; show.setDuration(showDuration); show.setInterpolator(i); show.setFillAfter(true); list.addOnScrollListener(new AutoHideListener(activity, view, hide, hideDuration, show, showDuration)); } }); }
三、呼叫方法
setViewAutoHide(this, btTextWantUpdate,recyclerView);
小節:
實現方法有多種,不過我剛好用到了他,大家共同學習進步