android:layout_height=“48dp”
app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.TitleBarBehavior”>
<TextView
…/>
<com.pengguanming.mimusicbehavior.widget.DrawableLeftTextView
…/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:id=“@+id/ll_content”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:translationY=“@dimen/content_trans_y”
app:layout_behavior=“com.pengguanming.mimusicbehavior.behavior.ContentBehavior”>
<com.flyco.tablayout.SlidingTabLayout
android:id=“@+id/stl”
…/>
<android.support.v4.view.ViewPager
android:id=“@+id/vp”
… />
</android.support.design.widget.CoordinatorLayout>
ContentBehavior
这个Behavior主要处理Content部分的Measure、嵌套滑动。
绑定需要做效果的View、引入Dimens、测量Content部分的高度
从上面图片能够分析出:
折叠状态时,Content部分高度=满屏高度-TopBar部分的高度
public class ContentBehavior extends CoordinatorLayout.Behavior{
private int topBarHeight;//topBar内容高度
private float contentTransY;//滑动内容初始化TransY
private float downEndY;//下滑时终点值
private View mLlContent;//Content部分
public ContentBehavior(Context context) {
this(context, null);
}
public ContentBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
//引入尺寸值
int resourceId = context.getResources().getIdentifier(“status_bar_height”, “dimen”, “android”);
int statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
topBarHeight= (int) context.getResources().getDimension(R.dimen.top_bar_height)+statusBarHeight;
contentTransY= (int) context.getResources().getDimension(R.dimen.content_trans_y);
downEndY= (int) context.getResources().getDimension(R.dimen.content_trans_down_end_y);
…
}
@Override
public boolean onMeasureChild(@NonNull CoordinatorLayout parent, View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec,int heightUsed) {
final int childLpHeight = child.getLayoutParams().height;
if (childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT
|| childLpHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
//先获取CoordinatorLayout的测量规格信息,若不指定具体高度则使用CoordinatorLayout的高度
int availableHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
if (availableHeight == 0) {
availableHeight = parent.getHeight();
}
//设置Content部分高度
final int height = availableHeight - topBarHeight;
final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height,
childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT
-
? View.MeasureSpec.EXACTLY
- View.MeasureSpec.AT_MOST);
//执行指定高度的测量,并返回true表示使用Behavior来代理测量子View
parent.onMeasureChild(child, parentWidthMeasureSpec,
widthUsed, heightMeasureSpec, heightUsed);
return true;
}
return false;
}
@Override
public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {
boolean handleLayout = super.onLayoutChild(parent, child, layoutDirection);
//绑定Content View
mLlContent=child;
return handleLayout;
}
}
实现NestedScrollingParent2接口
onStartNestedScroll()
ContentBehavior只处理Content部分里可滑动View的垂直方向的滑动。
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
@NonNull View directTargetChild, @NonNull View target, int axes, int type) {
//只接受内容View的垂直滑动
return directTargetChild.getId() == R.id.ll_content
&&axes== ViewCompat.SCROLL_AXIS_VERTICAL;
}
onNestedPreScroll()
接下来就是处理滑动,上面效果分析提过:
Content部分的:
上滑范围=[topBarHeight,contentTransY]、
下滑范围=[contentTransY,downEndY]即滑动范围为[topBarHeight,downEndY];
ElemeNestedScrollLayout要控制Content部分的TransitionY值要在范围内,具体处理如下:
Content部分里可滑动View往上滑动时:
-
如果Content部分当前TransitionY+View滑动的dy > topBarHegiht,设置Content部分的TransitionY为Content部分当前TransitionY+View滑动的dy达到移动的效果来消费View的dy。
-
如果Content部分当前TransitionY+View滑动的dy = topBarHegiht,同上操作。
-
如果Content部分当前TransitionY+View滑动的dy < topBarHegiht,只消费部分dy(即Content部分当前TransitionY到topBarHeight差值),剩余的dy让View滑动消费。
**Content部分里可滑动View往下滑动并且View已经不能往下滑动
(比如RecyclerView已经到顶部还往下滑)时:**
-
如果Content部分当前TransitionY+View滑动的dy >= topBarHeight 并且 Content部分当前TransitionY+View滑动的dy <= downEndY,设置Content部分的TransitionY为Content部分当前TransitionY+View滑动的dy达到移动的效果来消费View的dy
-
Content部分当前TransitionY+View滑动的dy > downEndY,只消费部分dy(即Content部分当前TransitionY到downEndY差值)并停止NestedScrollingChild2的View滚动。
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
float transY = child.getTranslationY() - dy;
//处理上滑
if (dy > 0) {
if (transY >= topBarHeight) {
translationByConsume(child, transY, consumed, dy);
} else {
translationByConsume(child, topBarHeight, consumed, (child.getTranslationY() - topBarHeight));
}
}
if (dy < 0 && !target.canScrollVertically(-1)) {
//处理下滑
if (transY >= topBarHeight && transY <= downEndY) {
translationByConsume(child, transY, consumed, dy);
} else {
translationByConsume(child, downEndY, consumed, (downEndY-child.getTranslationY()));
stopViewScroll(target);
}
}
}
private void stopViewScroll(View target){
if (target instanceof RecyclerView) {
((RecyclerView) target).stopScroll();
}
if (target instanceof NestedScrollView) {
try {
Class<? extends NestedScrollView> clazz = ((NestedScrollView) target).getClass();
Field mScroller = clazz.getDeclaredField(“mScroller”);
mScroller.setAccessible(true);
OverScroller overScroller = (OverScroller) mScroller.get(target);
overScroller.abortAnimation();
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
private void translationByConsume(View view, float translationY, int[] consumed, float consumedDy) {
consumed[1] = (int) consumedDy;
view.setTranslationY(translationY);
}
onStopNestedScroll()
在下滑Content部分从初始状态转换到展开状态的过程中松手就会执行收起的动画,这逻辑在onStopNestedScroll()实现,但注意如果动画未执行完毕手指再落下滑动时,应该在onNestedScrollAccepted()取消当前执行中的动画。
private static final long ANIM_DURATION_FRACTION = 200L;
private ValueAnimator restoreAnimator;//收起内容时执行的动画
public ContentBehavior(Context context, AttributeSet attrs) {
…
restoreAnimator = new ValueAnimator();
restoreAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
translation(mLlContent, (float) animation.getAnimatedValue());
}
});
}
public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
@NonNull View directTargetChild, @NonNull View target, int axes, int type) {
if (restoreAnimator.isStarted()) {
restoreAnimator.cancel();
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后
最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2019-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。
还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,也可以分享给身边好友一起学习。
一起互勉~
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
(img-tCwf8usj-1712527461607)]
[外链图片转存中…(img-GMx3G1dn-1712527461608)]
一起互勉~
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算