抽屉动画展开,倒计时Rxjava,解决Recyclerview纵向滑动事件冲突
先上效果图,不然我估计你们懒得看下去了...哈哈
这个是自定义控件,从大神那边copy过来修改成符合自己项目需求的,主要解决了第一次没有数据加载,后面当有数据刷新时没有显示问题。下面是动画的核心代码
/** * 动画 */ private void initAnimation() { if(notData){//没有数据则不走 return; } contentHeight = content.getMeasuredHeight(); hideAnimator = ValueAnimator.ofInt(contentHeight,0); hideAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { LayoutParams layoutParams = (LayoutParams) content.getLayoutParams(); layoutParams.height = (int) animation.getAnimatedValue(); content.setLayoutParams(layoutParams); } }); showAnimator = ValueAnimator.ofInt(0,contentHeight); myEvaluator.setStartValue(0); myEvaluator.setEndValue(contentHeight); showAnimator.setEvaluator(myEvaluator); showAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { LayoutParams layoutParams = (LayoutParams) content.getLayoutParams(); layoutParams.height = (int) animation.getAnimatedValue(); content.setLayoutParams(layoutParams); } }); }
后面找到原因是因为动画还没绘制之前就去获取高度,获取到的高度为0所以数据也就显示不出来了。
解决方法:
/** * 动画 */ private void initAnimation() { if(notData){//没有数据则不走 return; } contentHeight = content.getMeasuredHeight(); hideAnimator = ValueAnimator.ofInt(contentHeight,0); hideAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // Logger.e("========showAnimator=========="+animation.getAnimatedValue()); LayoutParams layoutParams = (LayoutParams) content.getLayoutParams(); layoutParams.height = (int) animation.getAnimatedValue(); content.setLayoutParams(layoutParams); } }); int size = content.getChildCount(); if(size>0 && !isShow){ View view = content.getChildAt(0); int height = view.getLayoutParams().height; contentHeight = size * height; } showAnimator = ValueAnimator.ofInt(0,contentHeight); myEvaluator.setStartValue(0); myEvaluator.setEndValue(contentHeight); showAnimator.setEvaluator(myEvaluator); showAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { LayoutParams layoutParams = (LayoutParams) content.getLayoutParams(); int mHeight = isAddOrDelete == false?getTargetHeight(content):(int)(getTargetHeight(content) * animation.getAnimatedFraction()); layoutParams.height = mHeight; content.setLayoutParams(layoutParams); } }); } getTargetHeight 就是通过反射的方法获取到view的高度,这样就可以获取到view的高度并且绘制出来了。 isAddOrDelete 这个属性是判断是否用户点击隐藏展示时用来控制是否开启动画的状态。
//反射获取控件高度 private int getTargetHeight(View v) { try { Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class); m.setAccessible(true); m.invoke(v, MeasureSpec.makeMeasureSpec( ((View) v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); } catch (Exception e) { } return v.getMeasuredHeight(); }
下面为项目源码路径:https://download.****.net/download/rookie_or_beginner/10982810