在使用BottomSheetBehavior实现底部弹窗我发现点击切换按钮没效果就不说了,居然还报错了,当时心情卧槽,到底哪里出问题了?
先看报错信息:
点击查看源码,找到抛出异常的代码:
看过之后发现要先设置隐藏,真是哔了狗了。。。
然后在布局中设置:
app:behavior_hideable="true"
完美解决问题,但是我手贱啊,想看看别的方案于是我在代码中设置:
//这种方式设置隐藏,报错
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
//这种方式设置隐藏,可以
bottomSheetBehavior.setHideable(true);
头疼,为什么?想了半天,关键代码在这一句:
if (!this.hideable || state != 5)
原因就是采用第一种方式设置隐藏,this.hideable默认为false,所以这行代码直接返回true,完美的避过了state!=5.
下面附完整代码:
public class MainActivity extends AppCompatActivity {
private BottomSheetDialog mBottomSheetDialog;
private List<String> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.tab_layout));
//设置默认先隐藏
bottomSheetBehavior.setHideable(true);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//根据状态不同显示隐藏
if(bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}else {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
});
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main4Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//按钮要使用布局包裹否则会浮在BottomSheetBehavior之上
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="打开BottomSheetBehavior" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_blue_dark"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="第一" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="第二" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="第三" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="第四" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
注意:app:behavior_peekHeight="0dp",peekHeight 属性是设置bottomSheet 折叠时的高度,我们设置为0表示折叠的时候完全隐藏,默认情况时显示布局的高度,布局会显示在界面,所以,如果要一开始布局不显示在界面上的话,需要将peekHeight 设置为0。也可以在代码中设置, 通过sheetBehavior.setPeekHeight(0)。