参考文档
- CoordinatorLayout 完全解析
1.1 关于NestedScrollingParent2、NestedScrollingChild2接口目的 支持滑动嵌套类的父布局 这篇文的demo挺好的
1.2 提供一个顶层布局来支持子View的协调交互,就完全解析里的floating样例很经典。
1.3 CoordinatorLayout和RecyclerView嵌套滑动冲突解决
1)对内部嵌套的横向滑动的RecyclerView 设置setNestedScrollingEnabled很好用 可以解滑动冲突,包括收起了的CoordinatorLayout margintop也拉不下来 贼好用
2)如果CollapsingToolbarLayout嵌套ConstrLayout会一起上移,ToolBar会固定住,CoordinatorLayout 能力有限,具体的还得自己做动画
3)AppLayoutBar和不动的ToolBar结合,偏移量会到ToolBar为止,下级依赖的高度不能比AppBarLayout大(不然收到的偏移量为0),实测相等是可以的
4)NestedScrollView高度必须要match_parent 不然影响上浮效果;
5)Activity的手势gesture可以不受影响,onTouchEvent会被拦截;
6)addOnOffsetChangedListener 设一个匿名对象remove时不好处理,但可以传this 以activity为实现对象,移除移this即可;
7)学习CoordinatorLayout你需要知道的那些事
- [CollapsingToolbarLayout说明] 可以实现折叠
2.1 他没有自带的监听,可以手动写监听,根据距离判断展开/收起
2.1 AppBarLayout:ToolBar一般定在最上面,这个可以使其滚动起来。
- FloatingActionButton基本使用及踩坑记录
扉
1. 初级样例传送门
- CollapsingToolbarLayout
- View - AppBarLayout(一)使用
- Android 详细分析AppBarLayout的五种ScrollFlags
- CollapsingToolbarLayout滑动状态监听
- CollapsingToolbarLayout实现自动折叠和展开
5.1 可设置动画,但是boolean类型
5.2 AppbarLayout最详细使用说明 - 内嵌view
- 利用 CollapsingToolbarLayout 完成联动的动画效果 结合距离公式
2. 细节注意
- androidx.coordinatorlayout.widget.CoordinatorLayout(协调者布局)包裹的内容
- AppBarLayout如果我们想要实现折叠的ActionBar效果
- CollapsingToolbarLayout主要是实现折叠布局的,需要作为AppBarLayout的子View才会有效
- AppbarLayout的简单用法
- 简单结构
oordinatorLayout orientation="vertical mm
AppBarLayout mh
CollapsingToolbarLayout mm app:layout_scrollFlags="scroll|exitUntilCollapsed"
ConstraintLayout mm
7. 其余各种属性详解
1)contentScrim :
当CollapsingToolbarLayout完全折叠后的背景颜色。
通常设置为:app:contentScrim=”?attr/colorPrimary”,这样当CollapsingToolbarLayout完全折叠后就会显示主题颜色。
2)layout_scrollFlags:
设置滚动表现:
1、 Scroll, 表示手指向上滑动的时候,CollapsingToolbarLayout也会向上滚出屏幕并且消失,这个属性必须要有。
2、 exitUntilCollapsed, 表示这个layout会一直滚动离开屏幕范围,直到它收折成它的最小高度.
8. 补充B站例子
修改Floating颜色
ColorStateList colorStateList = ContextCompat.getColorStateList(getApplicationContext(), R.color.red;
fabRandomCircle.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
fabRandomCircle.setBackgroundTintList(colorStateList);
加入Palette,一定要看
1. 安卓自带的Palette,需要Bitmap
1.5 补充
2. 操作
1.当一张图色彩区域不是很明显 设置默认颜色是有效的 如飞鼠
-
默认红色
-
默认蓝色
- 经过多张图片测试 主导色最好用 且测试发现 如果设置默认值的方式 返回的一定是颜色值,不传递默认值 返回的是Palette.Swatch对象。这个对象的方法很好用
class MainActivity : AppCompatActivity() {
var t1: TextView? = null
var t2: TextView? = null
var t3: TextView? = null
var t4: TextView? = null
var t5: TextView? = null
var t6: TextView? = null
var t7: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
t1 = findViewById<View>(R.id.t1) as TextView
t2 = findViewById<View>(R.id.t2) as TextView
t3 = findViewById<View>(R.id.t3) as TextView
t4 = findViewById<View>(R.id.t4) as TextView
t5 = findViewById<View>(R.id.t5) as TextView
t6 = findViewById<View>(R.id.t6) as TextView
t7 = findViewById<View>(R.id.t7) as TextView
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.mouse)
Palette.from(bitmap).generate(object : Palette.PaletteAsyncListener {
//发生主线程 Palette调色板 总共六种颜色
override fun onGenerated(palette: Palette?) {
palette?.let {
//柔和而暗的颜色
val darkMutedColor: Int = palette.getDarkMutedColor(Color.RED)
//鲜艳和暗的颜色
val darkVibrantColor: Int = palette.getDarkVibrantColor(Color.RED)
//亮和鲜艳的颜色
val lightVibrantColor: Int = palette.getLightVibrantColor(Color.RED)
//亮和柔和的颜色
val lightMutedColor: Int = palette.getLightMutedColor(Color.RED)
//柔和颜色
val mutedColor: Int = palette.getMutedColor(Color.RED)
val vibrantColor: Int = palette.getVibrantColor(Color.RED)
//返回Swatch主导颜色
val max: Palette.Swatch? = palette.dominantSwatch
t1!!.setBackgroundColor(darkMutedColor)
t2!!.setBackgroundColor(darkVibrantColor)
t3!!.setBackgroundColor(lightVibrantColor)
t4!!.setBackgroundColor(lightMutedColor)
t5!!.setBackgroundColor(mutedColor)
t6!!.setBackgroundColor(vibrantColor)
if (max != null) {
t7!!.setBackgroundColor(max.rgb)
t7!!.setTextColor(max.titleTextColor)
}
}
}
})
}
}