package com.dongnao.lsn_16_classdemo.design;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.NestedScrollingParent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import com.dongnao.lsn_16_classdemo.R;
import java.lang.reflect.Constructor;
@SuppressWarnings("all")
public class CoordinatorLayout extends RelativeLayout implements NestedScrollingParent, ViewTreeObserver.OnGlobalLayoutListener {
float lastX;
float lastY;
public CoordinatorLayout(Context context) {
super(context);
}
public CoordinatorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
LayoutParams lp = (LayoutParams) v.getLayoutParams();
if (lp.behavior != null)
lp.behavior.onSizeChanged(this, v, w, h, oldw, oldh);
}
}
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return true;
}
//当NestedScrollingChild发生滑动时,会调用此方法来通知NestedScrollingParent滑动了多少距离
//最常见的情景是,NestedScrollView通知CoordinatorLayout
//target:一般为NestedScrollView
//dyConsumed:NestedScrollView单次已消费滑动距离
//dyUnconsumed:NestedScrollView单次未消费滑动距离
//未消费的滑动距离是指,ScrollView已经滑动到顶端,手继续往下拉,但是控件内容缺没有继续滑动,手额外拉的这部分距离就是未消费的滑动距离
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
LayoutParams lp = (LayoutParams) v.getLayoutParams();
//关键代码,转发滑动事件
if (lp.behavior != null)
lp.behavior.onNestedScroll(target, v, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
}
}
@Override
public void onGlobalLayout() {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
LayoutParams lp = (LayoutParams) v.getLayoutParams();
if (lp.behavior != null)
lp.behavior.onLayoutFinish(this, v);
}
}
public class LayoutParams extends RelativeLayout.LayoutParams {
protected Behavior behavior;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.MyCoordinatorLayout);
String clazzName = a.getString(R.styleable.MyCoordinatorLayout_behavior);
behavior = parseBehaivor(context, attributeSet, clazzName);
a.recycle();
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(RelativeLayout.LayoutParams source) {
super(source);
}
protected Behavior parseBehaivor(Context context, AttributeSet attrs, String name) {
try {
Class<?> aClass = Class.forName(name, true, context.getClassLoader());
Constructor c = aClass.getConstructor(new Class[]{Context.class, AttributeSet.class});
c.setAccessible(true);
return (Behavior) c.newInstance(context, attrs);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
CoordinatorLayout源码解析和手动实现.zip
共34个文件
png:11个
xml:9个
java:5个
需积分: 5 0 下载量 121 浏览量
2022-03-31
14:46:39
上传
评论
收藏 1.16MB ZIP 举报
温馨提示
CoordinatorLayout源码解析和手动实现.zip
资源详情
资源评论
资源推荐
收起资源包目录
CoordinatorLayout源码解析和手动实现.zip (34个子文件)
demo-ui
gradlew.bat 2KB
gradlew 5KB
gradle.properties 89B
app
src
main
res
mipmap-xxxhdpi
main.png 1.03MB
ic_launcher.png 9KB
ic_launcher_round.png 15KB
drawable
ic_launcher_background.xml 6KB
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 208B
strings.xml 6KB
styles.xml 603B
mipmap-xxhdpi
ic_launcher.png 6KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 4KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher_round.xml 276B
ic_launcher.xml 276B
layout
activity_main.xml 2KB
java
com
dongnao
lsn_16_classdemo
design
CoordinatorLayout.java 4KB
ToolbarBehavior.java 1KB
Behavior.java 1KB
ImageBehavior.java 2KB
MainActivity.java 356B
AndroidManifest.xml 710B
build.gradle 917B
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 205B
local.properties 67B
settings.gradle 13B
build.gradle 1KB
共 34 条
- 1
命运之手
- 粉丝: 461
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- mybatis动态sql及其JAVA示例
- 微软常用运行库 游戏运行库 VC++各个版本
- 微信小程序开发教程.pptx
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 锐捷网络认证中心网络管理.pdf
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- SD8233LF是一款用于单按键触摸及接近感应开关,其用途是替代传统的机械型开关芯片IC
- 基于YOLOv5的烟雾火焰检测算法研究
- 基于STM32的联合调试侦听设备解决方案原理图PCB源文件调试工具视频(大赛作品)
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0