- CompoundButton 源码分析
- LinearLayout 源码分析
- SearchView 源码解析
- LruCache 源码解析
- ViewDragHelper 源码解析
- BottomSheets 源码解析
- Media Player 源码分析
- NavigationView 源码解析
- Service 源码解析
- Binder 源码分析
- Android 应用 Preference 相关及源码浅析 SharePreferences 篇
- ScrollView 源码解析
- Handler 源码解析
- NestedScrollView 源码解析
- SQLiteOpenHelper/SQLiteDatabase/Cursor 源码解析
- Bundle 源码解析
- LocalBroadcastManager 源码解析
- Toast 源码解析
- TextInputLayout
- LayoutInflater 和 LayoutInflaterCompat 源码解析
- TextView 源码解析
- NestedScrolling 事件机制源码解析
- ViewGroup 源码解析
- StaticLayout 源码分析
- AtomicFile 源码解析
- AtomicFile 源码解析
- Spannable 源码分析
- Notification 之 Android 5.0 实现原理
- CoordinatorLayout 源码分析
- Scroller 源码解析
- SwipeRefreshLayout 源码分析
- FloatingActionButton 源码解析
- AsyncTask 源码分析
- TabLayout 源码解析
BottomSheets 源码解析
2 月 25 日早上,Android 官网更新了 Support Lirary 23.2 版本,其中 Design Support Library 库新加一个新的东西:Bottom Sheets。然后,第一时间写了篇 Teach you how to use Design Support Library: Bottom Sheets ,只是简单的讲了它的使用和使用的一些规范。
这篇文章我带大家看看 BottomSheetBehavior 的源码,能力有限,写的不好的地方,请尽力吐槽。好了,不说废话,直接主题
我们先简单的看下用法
// The View with the BottomSheetBehavior
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//这里是 bottomSheet 状态的改变回调
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//这里是拖拽中的回调,根据 slideOffset 可以做一些动画
}
});对于切换状态,你也可以手动调用 behavior.setState(int state); state 的值你可以看我的上一篇 戳我
BottomSheetBehavior 的定义如下
public class BottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V>
继承自 CoordinatorLayout.Behavior, BottomSheetBehavior.from(V view) 方法获得了 BootomSheetBehavior 的实例,我们进去看看它怎么实现的。
public static <V extends View> BottomSheetBehavior<V> from(V view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (!(params instanceof CoordinatorLayout.LayoutParams)) {
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
}
CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
.getBehavior();
if (!(behavior instanceof BottomSheetBehavior)) {
throw new IllegalArgumentException(
"The view is not associated with BottomSheetBehavior");
}
return (BottomSheetBehavior<V>) behavior;
}源码中看出根据传入的参数 view 的 LayoutParams 是不是 CoordinatorLayout.LayoutParams,若不是,将抛出"The view is not a child of CoordinatorLayout"的异常,通过 ((CoordinatorLayout.LayoutParams) params).getBehavior() 获得一个 behavior 并判断是不是 BottomSheetBehavior,若不是,就抛出异常"The view is not associated with BottomSheetBehavior",都符合就返回了 BottomSheetBehavior 的实例。这里我们可以知道 behavior 保存在 CoordinatorLayout.LayoutParams 里,那它是 怎么保存的呢,怀着好奇心,我们去看看 CoordinatorLayout.LayoutParams 中的源码,在 LayoutParams 的构造函数中,有这么一句:
if (mBehaviorResolved) {
mBehavior = parseBehavior(context, attrs, a.getString(
R.styleable.CoordinatorLayout_LayoutParams_layout_behavior));
}顺藤摸瓜,我们在跟进去看看 parseBehavior 做了什么
static final Class<?>[] CONSTRUCTOR_PARAMS = new Class<?>[] {
Context.class,
AttributeSet.class
};
static Behavior parseBehavior(Context context, AttributeSet attrs, String name) {
/*
*省略部分代码
*/
try {
/*
*省略部分代码
*/
Constructor<Behavior> c = constructors.get(fullName);
if (c == null) {
final Class<Behavior> clazz = (Class<Behavior>) Class.forName(fullName, true,
context.getClassLoader());
c = clazz.getConstructor(CONSTRUCTOR_PARAMS);
c.setAccessible(true);
constructors.put(fullName, c);
}
return c.newInstance(context, attrs);
} catch (Exception e) {
throw new RuntimeException("Could not inflate Behavior subclass " + fullName, e);
}
}这里做的事情很简单,就是在实例化 CoordinatorLayout.LayoutParams 时反射生成 Behavior 实例,这就是为什么自定义 behavior 需要重写如下的构造函数
public class CjjBehavior extends CoordinatorLayout.Behavior{
public CjjBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
}不然就会看到"Could not inflate Behavior subclass ..."异常 。
目前为止,我们只是了解了 CoordinatorLayout.Behavior 相关的东西,还是不知道 BottomSheetBehavior 实现的原理,别急,这就和你说说。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论